public void sessionStarting(RequestContext context, FlowSession session, MutableAttributeMap<?> input) { boolean reusePersistenceContext = false; if (isParentPersistenceContext(session)) { if (isPersistenceContext(session.getDefinition())) { setHibernateSession(session, getHibernateSession(session.getParent())); reusePersistenceContext = true; } else { unbind(getHibernateSession(session.getParent())); } } if (isPersistenceContext(session.getDefinition()) && (!reusePersistenceContext)) { Session hibernateSession = createSession(context); setHibernateSession(session, hibernateSession); bind(hibernateSession); } }
public void sessionEnded(RequestContext context, FlowSession session, String outcome, AttributeMap<?> output) { if (isParentPersistenceContext(session)) { if (!isPersistenceContext(session.getDefinition())) { bind(getHibernateSession(session.getParent())); } } }
public void resuming(RequestContext context) { if (isPersistenceContext(context.getActiveFlow())) { bind(getHibernateSession(context.getFlowExecutionContext().getActiveSession())); } }
public void paused(RequestContext context) { if (isPersistenceContext(context.getActiveFlow())) { Session session = getHibernateSession(context.getFlowExecutionContext().getActiveSession()); unbind(session); session.disconnect(); } }
public void sessionStarting(RequestContext context, FlowSession session, MutableAttributeMap input) { 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(PERSISTENCE_CONTEXT_ATTRIBUTE, hibernateSession); bind(hibernateSession); } }
public void sessionEnding(RequestContext context, FlowSession session, String outcome, MutableAttributeMap<?> output) { if (isParentPersistenceContext(session)) { return; } if (isPersistenceContext(session.getDefinition())) { final Session hibernateSession = getHibernateSession(session); Boolean commitStatus = session.getState().getAttributes().getBoolean("commit"); if (Boolean.TRUE.equals(commitStatus)) { transactionTemplate.execute(new TransactionCallbackWithoutResult() { protected void doInTransactionWithoutResult(TransactionStatus status) { sessionFactory.getCurrentSession(); // nothing to do; a flush will happen on commit automatically as this is a read-write // transaction } }); } unbind(hibernateSession); hibernateSession.close(); } }
public void testFlowCommitsAfterMultipleRequests() { assertEquals("Table should only have one row", 1, getCount()); MockRequestContext context = new MockRequestContext(); MockFlowSession flowSession = new MockFlowSession(); flowSession.getDefinition().getAttributes().put("persistenceContext", "true"); hibernateListener.sessionStarting(context, flowSession, null); context.setActiveSession(flowSession); assertSessionBound(); TestBean bean1 = new TestBean("Keith Donald"); hibernate.templateSave(bean1); assertEquals("Table should still only have one row", 1, getCount()); hibernateListener.paused(context); assertSessionNotBound(); hibernateListener.resuming(context); TestBean bean2 = new TestBean("Keith Donald"); hibernate.templateSave(bean2); assertEquals("Table should still only have one row", 1, getCount()); assertSessionBound(); EndState endState = new EndState(flowSession.getDefinitionInternal(), "success"); endState.getAttributes().put("commit", true); flowSession.setState(endState); hibernateListener.sessionEnding(context, flowSession, "success", null); hibernateListener.sessionEnded(context, flowSession, "success", null); assertEquals("Table should only have three rows", 3, getCount()); assertSessionNotBound(); }
public void testNoCommitAttributeSetOnEndState() { assertEquals("Table should only have one row", 1, getCount()); MockRequestContext context = new MockRequestContext(); MockFlowSession flowSession = new MockFlowSession(); flowSession.getDefinition().getAttributes().put("persistenceContext", "true"); hibernateListener.sessionStarting(context, flowSession, null); context.setActiveSession(flowSession); assertSessionBound(); EndState endState = new EndState(flowSession.getDefinitionInternal(), "cancel"); flowSession.setState(endState); hibernateListener.sessionEnding(context, flowSession, "success", null); hibernateListener.sessionEnded(context, flowSession, "cancel", null); assertEquals("Table should only have three rows", 1, getCount()); assertSessionNotBound(); }
public void testSameSession() { MockRequestContext context = new MockRequestContext(); MockFlowSession flowSession = new MockFlowSession(); flowSession.getDefinition().getAttributes().put("persistenceContext", "true"); hibernateListener.sessionStarting(context, flowSession, null); context.setActiveSession(flowSession); assertSessionBound(); // Session created and bound to conversation final Session hibSession = (Session) flowSession.getScope().get("persistenceContext"); assertNotNull("Should have been populated", hibSession); hibernateListener.paused(context); assertSessionNotBound(); // Session bound to thread local variable hibernateListener.resuming(context); assertSessionBound(); hibernate.templateExecuteWithNativeSession(session -> assertSame("Should have been original instance", hibSession, session)); hibernateListener.paused(context); assertSessionNotBound(); }
private boolean isParentPersistenceContext(FlowSession flowSession) { return ((!flowSession.isRoot()) && isPersistenceContext(flowSession.getParent().getDefinition())); }
public void testLazyInitializedCollection() { MockRequestContext context = new MockRequestContext(); MockFlowSession flowSession = new MockFlowSession(); flowSession.getDefinition().getAttributes().put("persistenceContext", "true"); hibernateListener.sessionStarting(context, flowSession, null); context.setActiveSession(flowSession); assertSessionBound(); TestBean bean = hibernate.templateGet(TestBean.class, 0L); assertFalse("addresses should not be initialized", Hibernate.isInitialized(bean.getAddresses())); hibernateListener.paused(context); assertFalse("addresses should not be initialized", Hibernate.isInitialized(bean.getAddresses())); Hibernate.initialize(bean.getAddresses()); assertTrue("addresses should be initialized", Hibernate.isInitialized(bean.getAddresses())); }
@Override public void paused(RequestContext context) { if (LOG.isDebugEnabled()) LOG.debug("paused: Disconnecting Hibernate session"); super.paused(context); }
@Override public void sessionEnding(RequestContext context, FlowSession session, String outcome, MutableAttributeMap output) { final Session hibernateSession = getBoundHibernateSession(session); if (hibernateSession!= null && session.isRoot()) { LOG.debug("sessionEnding: Commit transaction and unbinding Hibernate session"); super.sessionEnding(context, session, outcome, output); } }
@Override public void resuming(RequestContext context) { if (!isSessionAlreadyBound()) { LOG.debug("resuming: Resumed flow, obtaining existing Hibernate session"); // final FlowExecutionContext executionContext = context.getFlowExecutionContext(); // if (executionContext.getActiveSession().getScope().get(PERSISTENCE_CONTEXT_ATTRIBUTE) != null) { super.resuming(context); // } } else { obtainCurrentSession(context); } }
@Override public void sessionEnded(RequestContext context, FlowSession session, String outcome, AttributeMap output) { if (isPersistenceContext(session.getDefinition()) && !isSessionAlreadyBound()) { super.sessionEnded(context, session, outcome, output); } }
public void exceptionThrown(RequestContext context, FlowExecutionException exception) { if (context.getFlowExecutionContext().isActive()) { if (isPersistenceContext(context.getActiveFlow())) { unbind(getHibernateSession(context.getFlowExecutionContext().getActiveSession())); } } }
public void sessionEnded(RequestContext context, FlowSession session, String outcome, AttributeMap output) { if (isPersistenceContext(session.getDefinition())) { 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() { protected void doInTransactionWithoutResult(TransactionStatus status) { sessionFactory.getCurrentSession(); // nothing to do; a flush will happen on commit automatically as this is a read-write // transaction } }); } unbind(hibernateSession); hibernateSession.close(); } if (!session.isRoot()) { FlowSession parent = session.getParent(); if (isPersistenceContext(parent.getDefinition())) { bind(getHibernateSession(parent)); } } }
public void resuming(RequestContext context) { if (isPersistenceContext(context.getActiveFlow())) { bind(getHibernateSession(context.getFlowExecutionContext().getActiveSession())); } }
public void sessionEnded(RequestContext context, FlowSession session, String outcome, AttributeMap<?> output) { if (isParentPersistenceContext(session)) { if (!isPersistenceContext(session.getDefinition())) { bind(getHibernateSession(session.getParent())); } } }
public void sessionEnding(RequestContext context, FlowSession session, String outcome, MutableAttributeMap<?> output) { if (isParentPersistenceContext(session)) { return; } if (isPersistenceContext(session.getDefinition())) { final Session hibernateSession = getHibernateSession(session); Boolean commitStatus = session.getState().getAttributes().getBoolean("commit"); if (Boolean.TRUE.equals(commitStatus)) { transactionTemplate.execute(new TransactionCallbackWithoutResult() { protected void doInTransactionWithoutResult(TransactionStatus status) { sessionFactory.getCurrentSession(); // nothing to do; a flush will happen on commit automatically as this is a read-write // transaction } }); } unbind(hibernateSession); hibernateSession.close(); } }