File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Handling Hibernate Session for Container Managed transactions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Handling Hibernate Session for Container Managed transactions" Watch "Handling Hibernate Session for Container Managed transactions" New topic

Handling Hibernate Session for Container Managed transactions

Srinivas Genji
Ranch Hand

Joined: May 04, 2004
Posts: 37
Hi Eveyryone,

I started using Hibernate and can any one explain how to handle Container Managed transaction. Here is my sample design

I have stateless session Bean and DAO (Hibernate) layer, session bean class has multiple DAO calls in a single session bean method. Reason behind Stateless session bean is to handle Container Managed Transaction. So Can any one help me how to handle CMT. How to create Hibernate Session, Can I use Session.openSession() or currentSession() ........... Provide some details on this.

Genji B Srinivas<br />M.Tech (IT) Aus
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17249

You will still code the same way, with openSession() then beginTransaction(), and then commit()/rollback() and close() when you are done with the session. Hibernate is smart enough to use the CMT transaction, instead of creating its own Transaction object. So the commit() will not commit the CMT transaction, because there might be more stuff the CMT is doing, Hibernate is smart to do the right thing, so all you have to do is code it just like it wasn't in CMT, the code looks the exact same, no differences.


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
sridhar skrishnan

Joined: Dec 24, 2007
Posts: 10
How does it work? I tried using CMTTransactionFactory. The app threw an exception when it encountered session.beginTransaction. However, If i take that code snippet off, its works fine....
I agree. Here's the link:
subject: Handling Hibernate Session for Container Managed transactions
Similar Threads
Handle exception in Facade
Transaction handling with Stateless SessionBean
Transaction between 2 methods in the same bean
Issues with Oracle connection/statement when accessing through JDBC
BMP transaction in DAO