wood burning stoves
The moose likes Object Relational Mapping and the fly likes Make hibernate use JTA transaction in WAS 5.1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Make hibernate use JTA transaction in WAS 5.1" Watch "Make hibernate use JTA transaction in WAS 5.1" New topic

Make hibernate use JTA transaction in WAS 5.1

jiayu gao

Joined: Jul 16, 2003
Posts: 9
Hibernate version:

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?
Matt DeC

Joined: Jun 22, 2003
Posts: 5
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.
Paulo Salgado
Ranch Hand

Joined: Jan 18, 2002
Posts: 98
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 ]
I agree. Here's the link: http://aspose.com/file-tools
subject: Make hibernate use JTA transaction in WAS 5.1
jQuery in Action, 3rd edition