Thanks Sebastian for your answer. I've read your post (15 jan 2006) and the answers...and your solution seems to work but does not fully satisfy me...lol
In the doc, section 3.8.3, it is stated:
...Using the "jta" session context, if there is no Hibernate Session associated with the current JTA transaction, one will be started and associated with that JTA transaction the first time you call sessionFactory.getCurrentSession()..
However, this section does not tell what happens when there is no JTA transaction in progress...
My understanding and experimentations with "jta" session context:
- openSession(): starts a new JTA transaction and a new Session (What happens when a JTA Transaction is in progress?)
- getCurrentSession():
- if a session is running, return it
- if ther is no session running, Hibernate tries to create one (section 3.8.3). If no JTA transaction is in progress , it will fail with exception "Current transaction is not in progress" If a JTA Transaction is running (Either created by the EJB container for EJB/CMT beans or manually for EJB/BMT or outside the EJB Container via the JTA UserTransaction interface, the session will hook to it
In both cases, the Transaction object returned by session.beginTransaction() should deal with the underlying JTA Transaction (commits, rollbacks).
What's still not clear to me:
- if the JTA transaction has been started because of a call to openSession(), will it be closed when calling: a) session.close() or b) tx.commit()?
of tx.commit()? close/commit the JTA transaction? (I understand that directly or indirectly calling UserTransaction.commit() will trigger some "commit callbacks" in the attached Hibernate session and transaction)
- if the session (re)used the JTA transaction in progress, what is the effect
What I will do is, as suggested in section 11.1.1 and by some user in this forum, is:
- use a filter to manage session.openSession() and session.close()
- use BMT for EJBs session facades that will call session.openSession(), then business/objects methods, and session.close()
- use session.getCurrentSession() anywhere else in the code
I would suggest having a table or two in the documentation explaining how the instances of UserTransaction, Session and Transaction interact in a jta environment. The tables could explain the effects of calling methods like session.openSession(), session.currentSession(), session.close(), transaction.commit(), UserTransaction.commit(), and this, if a JTA Transaction is in progress or not or if it has been started by Hibernate....etc. I thing this should be of great help (At least for me..)
And BTW Congratulations to everybody implied with the design/development of Hibernate. This product is fantastic..Thanks a lot