Version 8

    I get java.lang.IllegalStateException: Trying to change transaction ... in enlist

     

    JCA is giving me an error message saying it cannot enlist the connection in a transaction because it is already associated with a transaction.

     

    Answer

     

    You're playing with the thread/transaction association in ways that the application server doesn't understand. Most likely from BMT EJB or a web application.

     

    There is a feature associated with UserTransactions that lets you lazily enlist connections in user transactions.

     

    You've tried to suspend a UserTransaction and create a new one. JBoss doesn't understand this, it is not following JavaEE rules. Transactions should only be suspended when you cross EJB boundaries where the transaction manager can do the relevant book-keeping so it doesn't get confused.

     

    In pseudo code, the problem is effectively (This code is broken don't copy it!)

    DataSource ds = ...
    Connection c = ds.getConnection();
    UserTransaction ut = ...
    ut.begin(); // <======== Connection is enlisted here
    TransactionManager tm = ...
    tm.suspend();
    UserTransaction ut = ...
    ut.begin(); // <======== Tries to re-enlist the connection
    

     

    The second begin() will fail because the connection is already associated with the previous transaction which hasn't ended.

     

    Related