Make hibernate use JTA transaction in WAS 5.1

Hi guys, I am concerning how to convert the hibernate-based applicaction between JDBC transaction and JTA transaction transparently. And I have explored the hibernate source code and found out that, if we use a JTA transaction, hibernate will lookup a UserTransaction first and check if there is already another JTATransaction existing. If so, hibernate will not create a JTA transaction and join the existing one. So in this way, hibernate propose to make the application which uses hibernate transaction API can switch between JDBC and JTA transaction even CMT transparently without any code changing.

But in my application, I invode hibernate code in a Container Management Transaction Stateless Session Bean which is configured with Required transaction attribute in WAS 5.1. And in WAS 5.1, if a SLSB is CMT, the container will not provide a user transaction. That means we can't lookup a UserTransaction by "java:comp/UserTransaction". There will be an exception said that there is no comp/UserTransaction in java context. But hibernate does use this way to get JTA transaction. And I know I can configure the JTA transaction lookup name in cfg file, but the "jta/usertransaction" doesn't work with SLSB in WAS either.

So any knows how to make these kind of switching without changing code?

I had the exact same problem. My jUnit tests work fine. But the minute I attempted to call hibernate inside an ejb, I would get the error. I changed the bean's Transaction Type from container to Bean, and it is now working. Hibernate should handle the transaction piece, so I don't think I'll need to do anything else.

In theory you could create the UserTransaction before the EJB is called and define the EJB method with the "Supports" or "Required" transaction attribute. The EJB would use the same transaction context of the UserTransaction, which would also be available to Hibernate. [ July 14, 2005: Message edited by: Paulo Salgado ]