I was successful in doing this (which was a while ago) and sharing the solution in case someone else runs into this kind of a problem. Its a bit late, especially since 5.16 was released which handles this in a very graceful manner.
Had to create a CustomProcessEngineConfiguration class that extends ProcessEngineConfigurationImpl and overrides createTransactionInterceptor() and buildProcessEngine() methods.
<code>
@Singleton
public class CustomProcessEngineConfiguration extends ProcessEngineConfigurationImpl
{
@Override
protected CommandInterceptor createTransactionInterceptor()
{
return new TransactionInterceptor(this.getDataSource());
}
@Override
public ProcessEngine buildProcessEngine()
{
return super.buildProcessEngine();
}
}
</code>
The TransactionInterceptor class extends AbstractCommandInterceptor (for 'next' variable to execute the transaction chain) and implements org.aopalliance.intercept.MethodInterceptor. The overridden invoke(MethodInvocation invocation) method is what manages the starting and committing of transactions or rolling back in case of failure. In my case, it was as easy as looking at methods which have @com.google.inject.persist.Transactional annotation on them and wrapping them up in one transaction in the invoke() method.
On definition side, setting transactionsExternallyManaged to true in activiti.cfg.xml and creating process engine as:
<java>
ProcessEngine processEngine = CustomProcessEngineConfiguration
.createProcessEngineConfigurationFromResource("activiti.cfg.xml")
.setDataSource(dataSource)
.buildProcessEngine();
</java>
did the trick.