Keith Donald
2008-04-19 18:19:08 +00:00
parent 8cba9f13d8
commit 74e1a8ae46
5 changed files with 43 additions and 33 deletions

View File

@@ -104,13 +104,13 @@ public class HibernateFlowExecutionListener extends FlowExecutionListenerAdapter
}
public void sessionStarting(RequestContext context, FlowSession session, MutableAttributeMap input) {
if (isPersistenceContext(session.getDefinition())) {
if (!session.isRoot()) {
FlowSession parent = session.getParent();
if (isPersistenceContext(parent.getDefinition())) {
unbind(getHibernateSession(parent));
}
if (!session.isRoot()) {
FlowSession parent = session.getParent();
if (isPersistenceContext(parent.getDefinition())) {
unbind(getHibernateSession(parent));
}
}
if (isPersistenceContext(session.getDefinition())) {
Session hibernateSession = createSession(context);
session.getScope().put(HIBERNATE_SESSION_ATTRIBUTE, hibernateSession);
bind(hibernateSession);

View File

@@ -94,13 +94,13 @@ public class JpaFlowExecutionListener extends FlowExecutionListenerAdapter {
}
public void sessionStarting(RequestContext context, FlowSession session, MutableAttributeMap input) {
if (isPersistenceContext(session.getDefinition())) {
if (!session.isRoot()) {
FlowSession parent = session.getParent();
if (isPersistenceContext(parent.getDefinition())) {
unbind(getEntityManager(parent));
}
if (!session.isRoot()) {
FlowSession parent = session.getParent();
if (isPersistenceContext(parent.getDefinition())) {
unbind(getEntityManager(parent));
}
}
if (isPersistenceContext(session.getDefinition())) {
EntityManager em = entityManagerFactory.createEntityManager();
session.getScope().put(ENTITY_MANAGER_ATTRIBUTE, em);
bind(em);

View File

@@ -11,12 +11,12 @@ import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.HibernateTransactionManager;
import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.webflow.engine.EndState;
import org.springframework.webflow.engine.Flow;
import org.springframework.webflow.engine.builder.FlowAssembler;
import org.springframework.webflow.engine.builder.model.FlowModelFlowBuilder;
@@ -35,8 +35,6 @@ public class HibernateFlowManagedPersistenceIntegrationTests extends TestCase {
private SessionFactory sessionFactory;
private JdbcTemplate jdbcTemplate;
private HibernateTemplate hibernateTemplate;
private HibernateFlowExecutionListener hibernateListener;
@@ -46,7 +44,6 @@ public class HibernateFlowManagedPersistenceIntegrationTests extends TestCase {
protected void setUp() throws Exception {
DataSource dataSource = getDataSource();
populateDataBase(dataSource);
jdbcTemplate = new JdbcTemplate(dataSource);
sessionFactory = getSessionFactory(dataSource);
hibernateTemplate = new HibernateTemplate(sessionFactory);
hibernateTemplate.setCheckWriteOperations(false);
@@ -60,6 +57,9 @@ public class HibernateFlowManagedPersistenceIntegrationTests extends TestCase {
FlowAssembler assembler = new FlowAssembler(builder, context);
Flow flow = assembler.assembleFlow();
context.registerSubflow(flow);
Flow notManaged = new Flow("notmanaged");
new EndState(notManaged, "finish");
context.registerSubflow(notManaged);
context.registerBean("loadTestBean", new Action() {
public Event execute(RequestContext context) throws Exception {
assertSessionBound();
@@ -84,6 +84,13 @@ public class HibernateFlowManagedPersistenceIntegrationTests extends TestCase {
flowExecution.resume(context);
}
public void testManagedFlowWithUnmanagedSubflow() {
MockExternalContext context = new MockExternalContext();
flowExecution.start(null, context);
context.setEventId("notmanaged");
flowExecution.resume(context);
}
private DataSource getDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
@@ -132,10 +139,6 @@ public class HibernateFlowManagedPersistenceIntegrationTests extends TestCase {
return (SessionFactory) factory.getObject();
}
private void assertSessionNotBound() {
assertNull(TransactionSynchronizationManager.getResource(sessionFactory));
}
private void assertSessionBound() {
assertNotNull(TransactionSynchronizationManager.getResource(sessionFactory));
}

View File

@@ -10,13 +10,12 @@ import javax.sql.DataSource;
import junit.framework.TestCase;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTemplate;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.webflow.engine.EndState;
import org.springframework.webflow.engine.Flow;
import org.springframework.webflow.engine.builder.FlowAssembler;
import org.springframework.webflow.engine.builder.model.FlowModelFlowBuilder;
@@ -37,20 +36,14 @@ public class JpaFlowManagedPersistenceIntegrationTests extends TestCase {
private JpaFlowExecutionListener jpaListener;
private JdbcTemplate jdbcTemplate;
private JpaTemplate jpaTemplate;
private FlowExecution flowExecution;
protected void setUp() throws Exception {
DataSource dataSource = getDataSource();
populateDataBase(dataSource);
jdbcTemplate = new JdbcTemplate(dataSource);
entityManagerFactory = getEntityManagerFactory(dataSource);
JpaTransactionManager tm = new JpaTransactionManager(entityManagerFactory);
jpaListener = new JpaFlowExecutionListener(entityManagerFactory, tm);
jpaTemplate = new JpaTemplate(entityManagerFactory);
ClassPathResource res = new ClassPathResource("flow-managed-persistence.xml", getClass());
DefaultFlowModelHolder holder = new DefaultFlowModelHolder(new XmlFlowModelBuilder(res));
@@ -59,6 +52,9 @@ public class JpaFlowManagedPersistenceIntegrationTests extends TestCase {
FlowAssembler assembler = new FlowAssembler(builder, context);
Flow flow = assembler.assembleFlow();
context.registerSubflow(flow);
Flow notManaged = new Flow("notmanaged");
new EndState(notManaged, "finish");
context.registerSubflow(notManaged);
context.registerBean("loadTestBean", new Action() {
public Event execute(RequestContext context) throws Exception {
assertSessionBound();
@@ -74,7 +70,7 @@ public class JpaFlowManagedPersistenceIntegrationTests extends TestCase {
flowExecution = factory.createFlowExecution(flow);
}
public void testFlowWithSubflow() {
public void testManagedFlowWithManagedSubflow() {
MockExternalContext context = new MockExternalContext();
flowExecution.start(null, context);
context.setEventId("subflow");
@@ -83,6 +79,13 @@ public class JpaFlowManagedPersistenceIntegrationTests extends TestCase {
flowExecution.resume(context);
}
public void testManagedFlowWithUnmanagedSubflow() {
MockExternalContext context = new MockExternalContext();
flowExecution.start(null, context);
context.setEventId("notmanaged");
flowExecution.resume(context);
}
private DataSource getDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
@@ -131,10 +134,6 @@ public class JpaFlowManagedPersistenceIntegrationTests extends TestCase {
return factory.getObject();
}
private void assertSessionNotBound() {
assertNull(TransactionSynchronizationManager.getResource(entityManagerFactory));
}
private void assertSessionBound() {
assertNotNull(TransactionSynchronizationManager.getResource(entityManagerFactory));
}

View File

@@ -12,6 +12,7 @@
<view-state id="view">
<transition on="view2" to="view2"/>
<transition on="subflow" to="subflow"/>
<transition on="notmanaged" to="notmanaged"/>
<transition on="cancel" to="cancel"/>
<transition on="finish" to="finish"/>
</view-state>
@@ -21,6 +22,13 @@
<evaluate expression="loadTestBean" />
</transition>
</subflow-state>
<subflow-state id="notmanaged" subflow="notmanaged">
<transition on="finish" to="view2">
<evaluate expression="loadTestBean" />
</transition>
</subflow-state>
<view-state id="view2">
<transition on="finish" to="finish"/>