1 Reply Latest reply on Jun 15, 2010 6:09 AM by Michael Musgrove

    What order of operation when I use UserTransaction

    Michal Szymanski Newbie


      I would like to use pogramatic transaction, but I'm not sure when  UserTransaction should be created and when I should
      call .begin.
      Does order of opening connection matter? Should I write:
       
      UserTransaction utx = null;
      dic = new InitialContext();
      utx = (javax.transaction.UserTransaction) dic.lookup("UserTransaction");
      utx.begin();
      <opening daabase connection or geting from db pool>
      <database operation e.g. executeUpdate>
      utx.commit();

       

      or maybe

       

      UserTransaction utx = null;
      dic = new InitialContext();
      utx = (javax.transaction.UserTransaction) dic.lookup("UserTransaction");
      <opening database connection or geting from db pool>
      utx.begin();
      <database operation e.g. executeUpdate>
      utx.commit();

       

      Michal Szymanski

      http://techblog.freeconet.pl

      http://blog.szymanskich.net

        • 1. Re: What order of operation when I use UserTransaction
          Michael Musgrove Master

          If you are running inside an application server and obtaining the connection via the JBoss connection manager (which will be the case if you are using a JNDI lookup to obtain a datasource from which you obtain your connection, or are using injection) then the order does not matter.

           

          On the other hand if you are running as a remote client then you will need extra support to enlist JDBC resources (in this case UserTransaction just delagates transaction calls to the server and does not do anything with resources).

           

          So, I am answering this question from the point of view of the JEE specification and are assuming that you are using bean managed transactions (and are using JNDI or injection to obtain resources and transactions). When you obtain a connection, wrapper code checks to see if there is a transaction running and, if so, enlists it with the transaction. Alternatively, if you open a connection first and then start a transaction then the connection will be wrapped and when you invoke methods on the connection the wrapper will ensure that the resource is enlisted.

           

          Similarly, when you release the connection or terminate the transaction then the resource associated with the connection is delisted.