session && entityManager -> persistenceContext

This commit is contained in:
Keith Donald
2008-04-24 20:16:32 +00:00
parent 12423471b5
commit a1e7dc60ee
5 changed files with 22 additions and 20 deletions

View File

@@ -40,7 +40,8 @@ import org.springframework.webflow.execution.RequestContext;
* <p>
* The general pattern is as follows:
* <ul>
* <li>When a flow execution starts, create a new Hibernate Session and bind it to flow scope.
* <li>When a flow execution starts, create a new Hibernate Session and bind it to flow scope under the name
* {@link #PERSISTENCE_CONTEXT_ATTRIBUTE}.
* <li>Before processing a flow execution request, expose the conversationally-bound session as the "current session"
* for the current thread.
* <li>When an existing flow pauses, unbind the session from the current thread.
@@ -68,16 +69,16 @@ import org.springframework.webflow.execution.RequestContext;
* canceled before it ends. Generally, the only time a read-write transaction should be started is upon successful
* completion of the conversation, triggered by reaching a 'commit' end state.
*
* @author Ben Hale
* @author Keith Donald
* @author Juergen Hoeller
* @since 1.1
* @author Ben Hale
*/
public class HibernateFlowExecutionListener extends FlowExecutionListenerAdapter {
private static final String PERSISTENCE_CONTEXT_ATTRIBUTE = "persistenceContext";
private static final String HIBERNATE_SESSION_ATTRIBUTE = "session";
/**
* The name of the attribute the flow {@link Session persistence context} is indexed under.
*/
public static final String PERSISTENCE_CONTEXT_ATTRIBUTE = "persistenceContext";
private SessionFactory sessionFactory;
@@ -112,7 +113,7 @@ public class HibernateFlowExecutionListener extends FlowExecutionListenerAdapter
}
if (isPersistenceContext(session.getDefinition())) {
Session hibernateSession = createSession(context);
session.getScope().put(HIBERNATE_SESSION_ATTRIBUTE, hibernateSession);
session.getScope().put(PERSISTENCE_CONTEXT_ATTRIBUTE, hibernateSession);
bind(hibernateSession);
}
}
@@ -133,7 +134,7 @@ public class HibernateFlowExecutionListener extends FlowExecutionListenerAdapter
public void sessionEnded(RequestContext context, FlowSession session, String outcome, AttributeMap output) {
if (isPersistenceContext(session.getDefinition())) {
final Session hibernateSession = (Session) session.getScope().remove(HIBERNATE_SESSION_ATTRIBUTE);
final Session hibernateSession = (Session) session.getScope().remove(PERSISTENCE_CONTEXT_ATTRIBUTE);
Boolean commitStatus = session.getState().getAttributes().getBoolean("commit");
if (Boolean.TRUE.equals(commitStatus)) {
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@@ -177,7 +178,7 @@ public class HibernateFlowExecutionListener extends FlowExecutionListenerAdapter
}
private Session getHibernateSession(FlowSession session) {
return (Session) session.getScope().get(HIBERNATE_SESSION_ATTRIBUTE);
return (Session) session.getScope().get(PERSISTENCE_CONTEXT_ATTRIBUTE);
}
private void bind(Session session) {

View File

@@ -39,7 +39,8 @@ import org.springframework.webflow.execution.RequestContext;
* <p>
* This implementation uses standard JPA APIs. The general pattern is as follows:
* <ul>
* <li>When a flow execution starts, create a new JPA persistence context and bind it to flow scope.
* <li>When a flow execution starts, create a new JPA persistence context and bind it to flow scope under the name
* {@link #PERSISTENCE_CONTEXT_ATTRIBUTE}.
* <li>Before processing a flow execution request, expose the flow-scoped persistence context as the "current"
* persistence context for the current thread.
* <li>When an existing flow pauses, unbind the persistence context from the current thread.
@@ -71,13 +72,13 @@ import org.springframework.webflow.execution.RequestContext;
*
* @author Keith Donald
* @author Juergen Hoeller
* @since 1.1
*/
public class JpaFlowExecutionListener extends FlowExecutionListenerAdapter {
private static final String PERSISTENCE_CONTEXT_ATTRIBUTE = "persistenceContext";
private static final String ENTITY_MANAGER_ATTRIBUTE = "entityManager";
/**
* The name of the attribute the flow {@link EntityManager persistence context} is indexed under.
*/
public static final String PERSISTENCE_CONTEXT_ATTRIBUTE = "persistenceContext";
private EntityManagerFactory entityManagerFactory;
@@ -102,7 +103,7 @@ public class JpaFlowExecutionListener extends FlowExecutionListenerAdapter {
}
if (isPersistenceContext(session.getDefinition())) {
EntityManager em = entityManagerFactory.createEntityManager();
session.getScope().put(ENTITY_MANAGER_ATTRIBUTE, em);
session.getScope().put(PERSISTENCE_CONTEXT_ATTRIBUTE, em);
bind(em);
}
}
@@ -121,7 +122,7 @@ public class JpaFlowExecutionListener extends FlowExecutionListenerAdapter {
public void sessionEnded(RequestContext context, FlowSession session, String outcome, AttributeMap output) {
if (isPersistenceContext(session.getDefinition())) {
final EntityManager em = (EntityManager) session.getScope().remove(ENTITY_MANAGER_ATTRIBUTE);
final EntityManager em = (EntityManager) session.getScope().remove(PERSISTENCE_CONTEXT_ATTRIBUTE);
Boolean commitStatus = session.getState().getAttributes().getBoolean("commit");
if (Boolean.TRUE.equals(commitStatus)) {
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@@ -156,7 +157,7 @@ public class JpaFlowExecutionListener extends FlowExecutionListenerAdapter {
}
private EntityManager getEntityManager(FlowSession session) {
return (EntityManager) session.getScope().get(ENTITY_MANAGER_ATTRIBUTE);
return (EntityManager) session.getScope().get(PERSISTENCE_CONTEXT_ATTRIBUTE);
}
private void bind(EntityManager em) {

View File

@@ -75,7 +75,7 @@ public class HibernateFlowExecutionListenerTests extends TestCase {
assertSessionBound();
// Session created and bound to conversation
final Session hibSession = (Session) flowSession.getScope().get("session");
final Session hibSession = (Session) flowSession.getScope().get("persistenceContext");
assertNotNull("Should have been populated", hibSession);
hibernateListener.paused(context);
assertSessionNotBound();

View File

@@ -63,7 +63,7 @@ public class HibernateFlowManagedPersistenceIntegrationTests extends TestCase {
context.registerBean("loadTestBean", new Action() {
public Event execute(RequestContext context) throws Exception {
assertSessionBound();
Session session = (Session) context.getFlowScope().get("session");
Session session = (Session) context.getFlowScope().get("persistenceContext");
TestBean bean = (TestBean) session.get(TestBean.class, new Long(0));
assertNotNull(bean);
context.getFlowScope().put("testBean", bean);

View File

@@ -58,7 +58,7 @@ public class JpaFlowManagedPersistenceIntegrationTests extends TestCase {
context.registerBean("loadTestBean", new Action() {
public Event execute(RequestContext context) throws Exception {
assertSessionBound();
EntityManager em = (EntityManager) context.getFlowScope().get("entityManager");
EntityManager em = (EntityManager) context.getFlowScope().get("persistenceContext");
TestBean bean = (TestBean) em.getReference(TestBean.class, new Integer(0));
assertNotNull(bean);
context.getFlowScope().put("testBean", bean);