Igor Zelaya
Igor Zelaya

Reputation: 4297

closing jdbc connections in JTA

What will happen when DbConnection.close() is called for a XA Reource? Will it be returned to the connection pool or will it be held by the transaction manager until the Global Transaction finishes?

Upvotes: 2

Views: 1269

Answers (1)

Sam YC
Sam YC

Reputation: 11627

I have been cracking my head and looking for so many place for the answer, but none can give me a confirmation.

Most of time, the actual Transaction Manager behavior is vendor-dependent. But you can refer to the JTA 1.1 Specification too for the standard behavior.

  1. Assuming a client invokes an EJB bean with a TX_REQUIRED transaction attribute and the client is not associated with a global transaction, the EJB container starts a global transaction by invoking the TransactionManager.begin method.
  2. After the the transaction starts, the container invokes the bean method. As part of the business logic, the bean requests for a connection-based resource using the API provided by the resource adapter of interest.
  3. The application server obtains a resource from the resource adapter via some ResourceFactory.getTransactionalResource method.
  4. The resource adapter creates the TransactionalResource object and the associated XAResource and Connection objects.
  5. The application server invokes the getXAResource method.
  6. The application server enlists the resource to the transaction manager.
  7. The transaction manager invokes XAResource.start to associate the current transaction to the resource.
  8. The application server invokes the getConnection method.
  9. The application server returns the Connection object reference to the application.
  10. The application performs one or more operations on the connection.
  11. The application closes the connection.
  12. The application server delist the resource when notified by the resource adapter about the connection close.
  13. The transaction manager invokes XAResource.end to disassociate the transaction from the XAResource.
  14. The application server asks the transaction manager to commit the transaction.
  15. The transaction manager invokes XAResource.prepare to inform the resource manager to prepare the transaction work for commit.
  16. The transaction manager invokes XAResource.commit to commit the transaction.

enter image description here

So to answer you question, while you call the close(), the connection will be delisted, it is somehow still being hold by the Transaction Manager, so you can safely call close() before commit to pre-return the connection to the pool (if you worry about connection leak), although there are lot of Transaction Manager will help to auto-close the connection without you to manually close it (refer to Hibernate framework and Transaction Manager).

Upvotes: 3

Related Questions