How to provide my own transaction to jBPM?
helmers Apr 12, 2010 11:24 AMHi,
we're using jBPM 4.3, the jBPM tables are part of a bigger database schema, also containing our own tables.
Our transactional service method looks like this:
@Transactional
public String create(Issue issue) {
// 1. own stuff
issueDao.persist(issue);
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("ISSUE_ID", issue.getId());
// 2. jBPM stuff
ProcessInstance processInstance = executionService.startProcessInstanceByKey("PROCESS_KEY", variables);
// 3. jBPM stuff
List<Task> list = taskService.createTaskQuery().processInstanceId(processInstance.getId()).list();
// ... etc
}
First we write some stuff to our own tables, then we tell jBPM to start a process. This is all done within one transactional method (I was hoping...).
jbpm.cfg.xml:
<jbpm-configuration>
<import resource="jbpm.default.cfg.xml" />
<import resource="jbpm/tx.hibernate.cfg.xml" />
<import resource="jbpm.jpdl.cfg.xml" />
</jbpm-configuration>
tx.hibernate.cfg.xml:
<jbpm-configuration>
<process-engine-context>
<command-service name="txRequiredCommandService">
<skip-interceptor />
<retry-interceptor />
<environment-interceptor />
<standard-transaction-interceptor />
</command-service>
<command-service name="newTxRequiredCommandService">
<retry-interceptor />
<environment-interceptor policy="requiresNew" />
<standard-transaction-interceptor />
</command-service>
<hibernate-configuration>
<!-- Changed file name -->
<cfg resource="hibernate.cfg.xml" />
</hibernate-configuration>
<hibernate-session-factory />
</process-engine-context>
<transaction-context>
<transaction />
<hibernate-session />
</transaction-context>
</jbpm-configuration>
The problem...
The problem is that jBPM creates its own transaction, and does not take part in the transaction started by our own application (as you can see by writing an integration test that succeeds on steps [1] and [2], but uses a mockTaskService to throw an exception at [3] - what you see then is that our own tables are emptied (top-level transaction is rolled-back), but JBPM4_HIST_PROCINST is still filled, in other words [2] is executed in its own transaction and is not rolled back).
I tried to remove the "standard-transaction-interceptor" from tx.hibernate.cfg.xml hoping that jBPM would magically participate in my own transaction, but that doesn't make jBPM very happy.
Does anyone have an idea how I can tell jBPM to use my transaction, instead of starting its own?
I read in the jBPM 4 developer's guide:
http://docs.jboss.org/jbpm/v4/devguide/html_single/#d0e2216
that "User provided transasctional resources such as JDBC connections or hibernate connections" is not yet available in jBPM4. I guess this feature is what I need... is it already implemented or in the pipeline? Does anyone know when this feature will be available, or if there is a workaround for it?
This post is related to my earlier post on the fact that jbpm4 does not use the hibernate SessionFactory injected into it:
https://community.jboss.org/thread/150549?tstart=0
In the test project attached to that issue, you can also see how transaction management in our application works... Perhaps the problem is solved as soon as the SessionFactory injection issue is solved?
Cheers,
Guido