@Test
public void testCreationOfSessionWithinTransaction() throws Exception {

    System.setProperty("jbpm.tm.jndi.lookup", "java:comp/UserTransaction");
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get()
			.newDefaultBuilder()
            .userGroupCallback(userGroupCallback)
            .addAsset(ResourceFactory.newClassPathResource("BPMN2-ScriptTask.bpmn2"), ResourceType.BPMN2)
            .get();
    
    manager = RuntimeManagerFactory.Factory.get().newPerRequestRuntimeManager(environment);        
    assertNotNull(manager);
    
    UserTransaction ut = InitialContext.doLookup("java:comp/UserTransaction");
    ut.begin();
    
    RuntimeEngine runtime = manager.getRuntimeEngine(EmptyContext.get());
    KieSession ksession = runtime.getKieSession();
    assertNotNull(ksession);       
    
    long sessionId = ksession.getIdentifier();
    assertTrue(sessionId == 1);
    
    ut.commit();
    
    // since session was created with transaction tx sync is registered to dispose session
    // so now session should already be disposed
    try {
        ksession.getIdentifier();
        fail("Should fail as session manager was closed and with that it's session");
    } catch (IllegalStateException e) {
        
    } catch (UndeclaredThrowableException e) {
        TestUtil.checkDisposedSessionException(e);
    }
    System.clearProperty("jbpm.tm.jndi.lookup");
}