@Override public void sessionStarting(RequestContext context, FlowSession session, MutableAttributeMap input) { if (!isSessionAlreadyBound()) { LOG.debug("sessionStarting: Binding Hibernate session to flow"); super.sessionStarting(context, session, input); } else { LOG.debug("sessionStarting: Obtaining current Hibernate session"); obtainCurrentSession(context); } }
public void testFlowNotAPersistenceContext() { MockRequestContext context = new MockRequestContext(); MockFlowSession flowSession = new MockFlowSession(); hibernateListener.sessionStarting(context, flowSession, null); 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 testExceptionThrown() { 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.exceptionThrown(context, new FlowExecutionException("bla", "bla", "bla")); assertEquals("Table should still only have one row", 1, getCount()); assertSessionNotBound(); }
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())); }
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(); }
public void testFlowCommitsInSingleRequest() { 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 bean = new TestBean("Keith Donald"); hibernate.templateSave(bean); assertEquals("Table should still only have one row", 1, getCount()); 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 two rows", 2, getCount()); assertSessionNotBound(); }
public void testCancelEndState() { 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 bean = new TestBean("Keith Donald"); hibernate.templateSave(bean); assertEquals("Table should still only have one row", 1, getCount()); EndState endState = new EndState(flowSession.getDefinitionInternal(), "cancel"); endState.getAttributes().put("commit", false); flowSession.setState(endState); hibernateListener.sessionEnding(context, flowSession, "success", null); hibernateListener.sessionEnded(context, flowSession, "cancel", null); assertEquals("Table should only have two rows", 1, getCount()); assertSessionNotBound(); }
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(); }