-->

WARNING: executing transaction with 0 enlisted res

2019-09-15 05:58发布

问题:

I am getting this warning "executing transaction with 0 enlisted resource" while executing a distributed transaction with Bitronix into two datasources. Can some one help to understand how to enlist resources to global transaction. My code is:

   import java.sql.Connection;
   import java.sql.PreparedStatement;
   import java.sql.SQLException;

   import javax.sql.DataSource;
   import javax.transaction.HeuristicMixedException;
   import javax.transaction.HeuristicRollbackException;
   import javax.transaction.NotSupportedException;
   import javax.transaction.RollbackException;
   import javax.transaction.SystemException;

   import bitronix.tm.BitronixTransactionManager;
   import bitronix.tm.TransactionManagerServices;

   public class TestMain {

     public static void main(String[] args) {

     new TestMain().TestDT();

   }

   private void TestDT(){

     DataSource dataSourceRemote = ConnectionManager.getDatasourceRemote();
     DataSource dataSourceLocal = ConnectionManager.getDatasourceLocal();
     Connection con, conn = null;
     try {


        /*String remoteDS = "remoteDS";
        String localDS = "localDS";
        InitialContext ctxRemote = new InitialContext();
        ctxRemote.bind(remoteDS, dataSourceRemote);*/

        String INSERT_QUERY = "insert emp values (?,?,?,?)";

        /*InitialContext ctxLocal = new InitialContext();
        ctxLocal.bind(localDS, dataSourceLocal);*/
        BitronixTransactionManager btx =         TransactionManagerServices.getTransactionManager();
        //DataSource dsRemote = null;
        //btx.ge
    //UserTransaction us =  


        try {

            btx.begin();


            //DataSource dsRemote = (DataSource) ctxRemote.lookup(remoteDS);
            con = dataSourceRemote.getConnection();
            //con.setAutoCommit(false);
            PreparedStatement pstmt = con.prepareStatement(INSERT_QUERY);

            for(int i=1; i<=5; i++){
                pstmt.setInt(1, i);
                pstmt.setString(2, "Sanjay_"+i);
                pstmt.setString(3, "123"+i);
                pstmt.setString(4, "1000"+i);
                pstmt.execute();
            }



            //DataSource dsLocal = (DataSource) ctxLocal.lookup(localDS);
            conn = dataSourceLocal.getConnection();
            //conn.setAutoCommit(false);
            PreparedStatement ps = conn.prepareStatement(INSERT_QUERY);
            for(int i=1; i<=5; i++){
                ps.setInt(1, i);
                ps.setString(2, "Nikhil_"+i);
                ps.setString(3, "123"+i);
                ps.setString(4, "1000"+i);
                ps.execute();
            }

            btx.commit();
            con.close();
            conn.close();

        } catch (NotSupportedException e) {
            e.printStackTrace();
        } catch (SystemException e) {
            e.printStackTrace();
        }catch (SQLException e) {
            try{
                btx.rollback();
            }catch (Exception ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }catch (HeuristicRollbackException e) {
            try{
                btx.rollback();
            }catch (Exception ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }catch (HeuristicMixedException e) {
            try{
                btx.rollback();
            }catch (Exception ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }catch (RollbackException e) {
            try{
                btx.rollback();
            }catch (Exception ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }finally{
            //try{
                btx.shutdown();
                //con.close();
                //conn.close();
            /*}catch (Exception e) {
                e.printStackTrace();
            }*/
        }

        //btx.getTransaction()
    }catch (Exception e) {

        e.printStackTrace();
    }

}
      }

回答1:

"executing transaction with 0 enlisted resource" simply means that your data-sources are not mapped with Bitronix transaction manager.