Skip to content

Commit

Permalink
BZ-996205 - dispose sessions in PerProcessInstanceRuntimeManager
Browse files Browse the repository at this point in the history
(cherry picked from commit 3d9c853)
  • Loading branch information
mswiderski committed Aug 17, 2013
1 parent 58245e7 commit 0502bad
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 11 deletions.
Expand Up @@ -153,5 +153,15 @@ protected void removeRuntimeFromTaskService(InternalTaskService internalTaskServ
internalTaskService.removeMarshallerContext(getIdentifier());
}
}


protected boolean canDestroy() {
JtaTransactionManager tm = new ExtendedJTATransactionManager(null, null, null);
if (tm.getStatus() == JtaTransactionManager.STATUS_NO_TRANSACTION ||
tm.getStatus() == JtaTransactionManager.STATUS_ACTIVE) {
return true;
}
return false;
}

}
Expand Up @@ -55,7 +55,7 @@
* </ul>
*/
public class PerProcessInstanceRuntimeManager extends AbstractRuntimeManager {

private SessionFactory factory;
private TaskServiceFactory taskServiceFactory;

Expand Down Expand Up @@ -255,13 +255,8 @@ protected void removeLocalRuntime(RuntimeEngine runtime) {
@Override
public void init() {
// need to init one session to bootstrap all case - such as start timers
RuntimeEngine engine = getRuntimeEngine(EmptyContext.get());
configureRuntimeOnTaskService((InternalTaskService) engine.getTaskService());
try {
engine.getKieSession().destroy();
} finally {
disposeRuntimeEngine(engine);
}
KieSession ksession = factory.newKieSession();
ksession.destroy();

}

Expand Down
Expand Up @@ -25,8 +25,6 @@
import org.kie.internal.runtime.manager.RuntimeEnvironment;
import org.kie.internal.runtime.manager.SessionFactory;
import org.kie.internal.runtime.manager.TaskServiceFactory;
import org.kie.internal.runtime.manager.context.EmptyContext;
import org.kie.internal.task.api.ContentMarshallerContext;
import org.kie.internal.task.api.InternalTaskService;

/**
Expand Down Expand Up @@ -84,7 +82,13 @@ public void validate(KieSession ksession, Context<?> context) throws IllegalStat
public void disposeRuntimeEngine(RuntimeEngine runtime) {
local.set(null);
try {
runtime.getKieSession().destroy();
if (canDestroy()) {
runtime.getKieSession().destroy();
} else {
if (runtime instanceof Disposable) {
((Disposable) runtime).dispose();
}
}
} catch (Exception e) {
// do nothing
if (runtime instanceof Disposable) {
Expand Down
Expand Up @@ -99,4 +99,8 @@ public void setManager(RuntimeManager manager) {
this.manager = manager;
}

public boolean isDisposed() {
return disposed;
}

}
Expand Up @@ -5,9 +5,13 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.lang.reflect.UndeclaredThrowableException;
import java.util.List;
import java.util.Properties;

import javax.naming.InitialContext;
import javax.transaction.UserTransaction;

import org.jbpm.process.audit.AuditLogService;
import org.jbpm.process.audit.JPAAuditLogService;
import org.jbpm.process.audit.ProcessInstanceLog;
Expand All @@ -31,6 +35,7 @@
import org.kie.internal.runtime.manager.RuntimeEnvironment;
import org.kie.internal.runtime.manager.RuntimeManagerFactory;
import org.kie.internal.runtime.manager.context.CorrelationKeyContext;
import org.kie.internal.runtime.manager.context.EmptyContext;
import org.kie.internal.runtime.manager.context.ProcessInstanceIdContext;
import org.kie.internal.task.api.UserGroupCallback;

Expand Down Expand Up @@ -383,4 +388,30 @@ public void testStartTwoProcessIntancesOnSameSession() {
}
manager.close();
}

@Test
public void testCreationOfRuntimeManagerWithinTransaction() throws Exception {
System.setProperty("jbpm.tm.jndi.lookup", "java:comp/UserTransaction");

UserTransaction ut = InitialContext.doLookup("java:comp/UserTransaction");
ut.begin();
RuntimeEnvironment environment = RuntimeEnvironmentBuilder.getDefault()
.userGroupCallback(userGroupCallback)
.addAsset(ResourceFactory.newClassPathResource("BPMN2-ScriptTask.bpmn2"), ResourceType.BPMN2)
.get();

manager = RuntimeManagerFactory.Factory.get().newPerProcessInstanceRuntimeManager(environment);
assertNotNull(manager);


RuntimeEngine runtime = manager.getRuntimeEngine(EmptyContext.get());
KieSession ksession = runtime.getKieSession();
assertNotNull(ksession);

ksession.startProcess("ScriptTask");

ut.commit();

System.clearProperty("jbpm.tm.jndi.lookup");
}
}
Expand Up @@ -225,4 +225,30 @@ public void testExecuteReusableSubprocess() {

logService.dispose();
}

@Test
public void testCreationOfRuntimeManagerWithinTransaction() throws Exception {
System.setProperty("jbpm.tm.jndi.lookup", "java:comp/UserTransaction");

UserTransaction ut = InitialContext.doLookup("java:comp/UserTransaction");
ut.begin();
RuntimeEnvironment environment = RuntimeEnvironmentBuilder.getDefault()
.userGroupCallback(userGroupCallback)
.addAsset(ResourceFactory.newClassPathResource("BPMN2-ScriptTask.bpmn2"), ResourceType.BPMN2)
.get();

manager = RuntimeManagerFactory.Factory.get().newPerRequestRuntimeManager(environment);
assertNotNull(manager);


RuntimeEngine runtime = manager.getRuntimeEngine(EmptyContext.get());
KieSession ksession = runtime.getKieSession();
assertNotNull(ksession);

ksession.startProcess("ScriptTask");

ut.commit();

System.clearProperty("jbpm.tm.jndi.lookup");
}
}

0 comments on commit 0502bad

Please sign in to comment.