cancel
Showing results for 
Search instead for 
Did you mean: 

handle transactions externally with hibernate

gguimezanes
Champ in-the-making
Champ in-the-making
Hello,
I am using activiti within an existing application that uses Hibernate, with no JTA, transactions are handled "manually".
I would like to use activiti inside our own transactions so that a rollback would work at the same time on our custom code and on activiti.
I have seen the transactionsExternallyManaged parameter in ProcessEngineConfiguration, but as I understand it, it seems to be only used for JTA.
Is there any way I could pass directly my hibernate transactions (or its underlying connection) to activiti?
Thanks
6 REPLIES 6

jbarrez
Star Contributor
Star Contributor
If you are using Spring, it makes things easier as you can just pass the transactionmanager to the process engine configuration.

The transactionsExternallyManaged is actually used to determine which transaction factory is used in Ibatis. So my guess it would work, however, I haven't tested it yet with Hibernate alone.

gguimezanes
Champ in-the-making
Champ in-the-making
The problem is, we don't use Spring. We don't have a TransactionManager, we directly use the hibernate session. That's why I was wondering if we could pass directly the java.sql.Connection to activiti…

frederikherema1
Star Contributor
Star Contributor
Activiti uses a datasource for fetching SQL-connections. If you have a datasource (or proxy) that is aware of the hibernate-transaction and always returns the SQL-connection active in the hibernate-context, this can work perfectly, in combination with the "transactions managed externally".

ronald_van_kuij
Champ on-the-rise
Champ on-the-rise
I've tried and achieved sometinhg similar, but can't and won't share the code… Why: if you want integration of frameworks, use the existing solutions for that, a transactionmanager. Waaaaay reasier, portable etc…

gguimezanes
Champ in-the-making
Champ in-the-making
In fact just using a custom datasource wasn't enough.
The solution that worked for us :
- create a Datasource that always gives the connection of our current hibernate session
- create an engine configuration that overrides initDataSource and initDatabaseType
- set transactionExternallyManaged to true
- create our own TransactionContext that does neither commit nor rollback (but tells our system that the transaction is not committable when the method rollback is called)
- add "closeConnection" =  "false" to the properties of the transaction factory

Hope this helps if someone else has the same need…

ronald_van_kuij
Champ on-the-rise
Champ on-the-rise
Cool isn't it, these details… Soooo much simpeler than just using a JEE server with a normal TM  Smiley Wink