1 Reply Latest reply on Jul 3, 2006 12:29 PM by Alan Feng

    How to obtain the UserTransaction object in a session bean?

    Alan Feng Novice

      Just started with JBoss + EJB 3 + Hibernate.

      I have some simle SessionBean object, in which it obtains Hibernate Session object and performs db related operations. I configured the Hibernate to use JTA from JBoss by specifying Hibernate property jta.UserTransaction=UserTransaction. Then I found that my transaction is not committed, nor rolled back on exception.

      It turns out that the UserTransaction object bound to the JNDI name UserTransaction from JBoss is a global object that get reused again and again in different invocation of the SessionBean methods. I verified this by writing a simple add(int a, int b) method which does not use Hibernate at all and prints the System.identityHash(UserTransaction) to the console.

      I did try to set the transaction attribute of the method to be REQUIRES_NEW but no luck.

      It is aparently some basic things I am doing wrong. How do I obtain the transaction object that is associated with SessionBean method invocation? The transaction object that will auto rollback when I throw an exception and auto commit when the method exits properly?

      I searched posts and googled a round for a long time but didn't find the answer. Thanks in advance for any help!

        • 1. Re: How to obtain the UserTransaction object in a session be
          Alan Feng Novice

          Just to make this thread complete.

          I've solved the problem after lots of research. The problem was actually caused by that I specified the JDBC connection information in the Hibernate configuration file directly. In other words, Hibernate uses the JDBC directly without letting the JBoss App Server to manage it. Therefore, JBoss never know there is such a JDBC data source exists and can't do commit/rollback.

          To solve it, I have to create a mydb-ds.xml file and place it under the meta-inf dir of my EAR. Then in the jboss-app.xml file, I refer to it as:


          Then configure the Hibernate to use JNDI to get the reference of the data source (plenty of doc on this). Also note that the ${JBOSS_HOME}/doc/examples/jca contains lots of sample xxx-ds.xml file as template.

          It seems that Hibernate document failed to mention this necessary configuration change when using JTA, and I see lots of posts with similary problem but no answers. It really should be added into the Hibernate doc.