Transactions and Listeners

342 views
Skip to first unread message

Cristian Mastrantono

unread,
Feb 27, 2015, 1:09:38 PM2/27/15
to camunda-...@googlegroups.com
Hello everyone, 

I'm wondering if it is possible to put a listener somewhere to get alerted when a transaction ends successfully. This is, imagine you have a User Task and a Service Task after. When User Task is completed, the Service Task is invoked and the transaction finishes when the Service Task finishes. 

Putting some listeners in the User Task  (complete and delete task or end execution listeners)  does not work because this are invoked when the task completed, and not the transaction.

If there is no way you can "listen" when a transaction finishes... in case of rollback the Start or Create events of the User Task will be fired again ??.

The point is, I need to run some code when the transaction is successful or -failing that- to execute that code when Task is completed but rest assured that if the transaction fails then the Create or Start the events on the User Task will be triggered again.

Thanks for your time!
Cristian.

webcyberrob

unread,
Feb 27, 2015, 8:37:50 PM2/27/15
to camunda-...@googlegroups.com
Hi,

Could you put the user task and the service task inside a BPMN transaction and a second service task outside the BPMN transaction? Hence the second service task only runs if the first two tasks are successful.

regards

Rob

Cristian Mastrantono

unread,
Mar 2, 2015, 8:17:29 AM3/2/15
to camunda-...@googlegroups.com
Well, I was thinking that according to transaction boundaries, the engine will return the control to the app after the transaction finises, so maybe that could be my "listener".

Like: taskService.complete(taskId); --> After this call, the transaction is completed.

I'm right?

Daniel Meyer

unread,
Mar 2, 2015, 8:25:05 AM3/2/15
to camunda-...@googlegroups.com
1)

you can register transaction listeners from "within" the process engine:

Context.getCommandContext()
.getTransactionContext()
.addTransactionListener(TransactionState.COMMITTED, new
TransactionListener() {

public void execute(CommandContext commandContext) {
// called after commit
}
});



2)

if the transaction is managed through spring or JTA, it may commit AFTER
the command has returned. Example (Spring):

public class SomeBean {

@Transactional
public void myMethod() {

// do some transactional work

taskService.completeTask(); // Camunda paticipates in existing
Spring managed transaction

// do additional work

} //<< transaction may commit here or even further up the call stack

}

Cheers,
Daniel

Reply all
Reply to author
Forward
0 new messages