Wednesday, June 24, 2009

Mixing transactions manual and container managed with JTA

How-to Mix manual transactions and container managed transactions

Container managed transactions use JTA for transaction management. If we need to mix the manual transactions and container managed transactions we have to use the JTA for the manual transactions as well.

You can use the following two classes for handling manual transactions with JTA.

Listing : Class CustomTransactionCallbackWithoutResult

public abstract class CustomTransactionCallbackWithoutResult {

   public abstract void doInTransactionWithoutResult();


Listing : Class CustomTransactionTemplate

import javax.transaction.UserTransaction;

import javax.transaction.SystemException;

public class CustomTransactionTemplate {


UserTransaction ut;

    public CustomTransactionTemplate(UserTransaction ut) {

        this.ut = ut;

    public void execute(CustomTransactionCallbackWithoutResult transactionCallbackWithoutResult) {

            try {

              try {



            } catch (Throwable ex) {

                try {


                } catch (SystemException syex) {

                  throw new Exception( "Rollback failed: " + syex.getMessage());


                 throw new Exception( "Transaction failed: " + ex.getMessage());


        } catch (Throwable e) {

            throw new RuntimeException(e);


Using the above two classes


InitialContext ic = new InitialContext();

UserTransaction ut = (UserTransaction) c.lookup("java:comp/UserTransaction");

final DataSource ds = (DataSource) c.lookup("java:/sampleDS");


CustomTransactionTemplate txTemplate = new CustomTransactionTemplate(ut);

txTemplate.execute(new CustomTransactionCallbackWithoutResult() {

   public void doInTransactionWithoutResult() {

     //We are in the transaction
                        // Use ds and update a table

     // call a session bean which in turn call Entity bean

    // and cause Container  managed transactions                     





When mixing container managed transactions and manual transactions if  you try to use the setAutoCommit() or Hibernate TransactionTemplate the following errors will be fired. To avoid the error the best bet is to use the JTA transactions in every place.


java.sql.SQLException: Connection handle has been closed and is unusable at org.jboss.resource.adapter.jdbc.WrappedConnection.checkStatus( at org.jboss.resource.adapter.jdbc.WrappedConnection.checkTransaction( at org.jboss.resource.adapter.jdbc.WrappedConnection.prepareStatement( at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement( at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement( at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement( at org.hibernate.loader.Loader.prepareQueryStatement( at org.hibernate.loader.Loader.doQuery( at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections( at org.hibernate.loader.Loader.doList(     


1 comment:

Anonymous said...


Post a Comment