Discussions

EJB design: Closing connections inside a JTA transaction in an EJB

  1. If I have a JDBC connection provided by a J2EE container (via JNDI and a pooled Datasource) and am in the middle of a transaction, what happens if I call connection.close() - does it commit the transaction and return the conneciton to the pool?

    Or, does it return the connection to the pool, and keep the transaction in the current state, so that things can continue in the current transaction?
  2. As per my understanding, if you use Declarative Transaction, EJB Server's Transaction Manager is responsible for making decision to either commit or rollback the transaction. I think the way it works is, when you set a transaction scope attribute for a bean's method, the EJB Object registers the bean with the Transaction Manager. Transaction Manager will create a Transactional Context for the registered bean based on the transactional scope.

    The transaction will be propagated to all the methods invoked from the first ejb method. Finally the Transaction manager will check based on Exceptions, if no exceptions it will commit otherwise it will rollback the transaction. If the beans are in multiple EJB Servers, it will use Distributed Transaction management(2Phase Commit).

    So, I think connection.close() should not have any effect on transaction in EJB.

    Hope this helps,
    Senthil.