Version 6

    Lazy JCA Enlistment

     

    There is a little known feature of JavaEE where you can do things in the "wrong order" for UserTransactions and it still does the transaction enlistment.

     

    CMT enlistment

     

    Normally in JavaEE you use CMT where the transaction is already started before your method begins you can then do

    DataSource ds = ...
    Connection c = ds.getConnection(); // <======= Connection is enlisted in transaction here
    

    this is called automatic enlistment.

     

    Similar BMT enlistment

     

    With a UserTransaction you are in control of the transaction so this works as well

    UserTransaction ut = ...
    ut.begin();
    DataSource ds = ...
    Connection c = ds.getConnection(); // <======= Connection is enlisted in transaction here
    

     

    Lazy BMT enlistment

     

    What is not often appreciated is that you can do it the "wrong way around"

    DataSource ds = ...
    Connection c = ds.getConnection();
    UserTransaction ut = ...
    ut.begin();  // <======= Connection is enlisted in transaction here
    

     

    This is so you can do

    DataSource ds = ...
    Connection c = ds.getConnection();
    UserTransaction ut = ...
    ut.begin();  // <======= Connection is enlisted in transaction here
    // Do work
    ut.commit(); // <======= Connection is de-enlisted here but not returned to the pool
    ut.begin();  // <======= Connection is enlisted in the new transaction here
    

     

    Implementation

     

    To facilitate this feature, the application server keeps track of all the connections opened for

    BMT beans and webapps and whenever you start a user transaction it enlists them automatically,

    it does this for every user transaction you create.