JBoss transaction management


Posted:   |  More posts about java

Couple of the months ago an architect from offshore development team suggested a new (to me at least!) way of handling nested SQL transactions in J2EE applications: create connection in Stateless Session bean and pass it to all DAO code. Being an J2EE architect myself I rejected that approach mainly due to the fact that I don't like to keep connection open for a long time in J2EE apps - it's reduces concurrency and it's a bad thing to do in general. I also have told offshore guys that J2EE App server is going to handle transactions a proper way.

Today I decided to look into JBoss source code to find out how transaction management is done inside. At first I have looked into transaction management itself. The code there is very simple: if, for example, commit is required to be executed - execute commit on all resources and collect results. But to which resources commit can be executed? Connection, probably... So I started looking for a methods and classes responsible to create and manage connections. And this is what I found in JBossManagedConnectionPool.java:

    public ConnectionListener getConnection(Transaction transaction, Subject subject, ConnectionRequestInfo cri)
       throws ResourceException
    {
       // Determine the pool key for this request
       boolean separateNoTx = false;
       if (noTxSeparatePools)
          separateNoTx = clf.isTransactional();
       Object key = getKey(subject, cri, separateNoTx);
       InternalManagedConnectionPool mcp = getPool(key, subject, cri);
   // Are we doing track by connection?
   TransactionLocal trackByTx = null;
   if (transaction != null)
      trackByTx = (TransactionLocal) trackByTxPools.get(key);

   // Do we have a previous connection in this transaction?
   if (trackByTx != null)
   {
      ConnectionListener cl = (ConnectionListener) trackByTx.get(transaction);
      if (cl != null)
      {
         if (traceEnabled)
            dump("Getting connection tracked by transaction " + cl);
         return cl;
      }
   }

.....

}

That piece of code answers my question: if code requests a new Connection object while inside transaction - it will be given the same connection as to the other objects inside that transaction. There is no need to pass the same connection object to all nested DAO methods, it's done automatically by JBoss.

Now I know that for sure!

Contents © 2013 Aleksey Maksimov - Powered by Nikola