In EJB context does transaction mngr releases the connection ?

Discussions

EJB programming & troubleshooting: In EJB context does transaction mngr releases the connection ?

  1. Hi All,

    I've a doubt regarding release of connection in transaction handling scenarios in EJB.

    Let us assume I have an EJB method associated with a transaction attribute as Requires New (i.e. the ejb method is associated with container managed transaction).
    Within that method two diffreent databasess are accessed, connections are created, databases are updated but connections are not realeased.

    The code goes similar to the one given below -


    public void beanMethod1()

    {

    InitialContext cntx = new InitialContext();
    javax.sql.DataSource ds1 = cntx.lookup("dataSourceName1");

    javax.sql.DataSource ds2 = cntx.lookup("dataSourceName2");

    java.sql.Connection conn1 = ds1.getConnection();
    ds1.setAutoCommit(false);

    PreparedStatement pst1 = ds1.prepareStatement("Qyery1");
    pst1.executeUpdate();

    java.sql.Connection conn2 = ds2.getConnection();
    ds2.setAutoCommit(false);

    PreparedStatement pst2 = ds2.prepareStatement("Qyery2");
    pst2.executeUpdate();

    }


    Now in this context my doubt is - will the transaction manager, along with handling commit/rollback, release the connections also (once commit/rollback is over) ? Or releasing of connections need to be handled in the bean method ?

    If releasing connections has to be handled in the bean method, then how does transaction manager execute a commit/rollback on a released connection ?

    The same doubt can be extended to bean managed transaction also where transaction boundarry is demarcated using javax.transaction.UserTransaction object's begin(), commit() and rollback() methods.

    It will be a real help if anyone please throw some light on this doubt.

    Thanks in advance,
    Sourav




  2. Q) Now in this context my doubt is - will the transaction manager, along with handling commit/rollback, release the connections also (once commit/rollback is over) ? Or releasing of connections need to be handled in the bean method ?

    A) No you must release the connection to the cache by calling close();

    Q) If releasing connections has to be handled in the bean method, then how does transaction manager execute a commit/rollback on a released connection ?

    A) Connection.close() is logical in EJB. The connection isnt actually closed but returned to the cache. The tx manager and the cache work together. When close() is called, the tx manager will determine if a commit/rollback is needed, do that, then return the connection to the pool if appropriate.

    Q) The same doubt can be extended to bean managed transaction also where transaction boundarry is demarcated using javax.transaction.UserTransaction object's begin(), commit() and rollback() methods.

    A) Here it is a bit different. The developer must call commit() before close();


    Dave Wolf
    The Scupper Group
    dave at scuppergroup dot com