• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do Container Managed Transactions work?  RSS feed

 
T Stevens
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been trying to get my head around CMT and been struggling to work out how the Container does the management.

Assume that I'm doing simple non-XA single-datasource database access in EJB 3.0 and configured the Stateless Session Bean using @TransactionManagement(TransactionManagementType.CONTAINER)

So, when I call a method (methodA), I decide that I need a database connection. So, I use a JNDI lookup to obtain the datasource, get a new connection and do some SQL. The spec says that I mustn't do a commit on the connection, so I finish the code with a connection.close().


My question is, how does the connection know to participate in the transaction that is defined at the SSB level. Presumably, methodA can call methodB which could call similar code, again connecting to the database and doing some more SQL. How does the container co-ordinate this given that I have closed the connection myself?
 
Jerwin Louise Uy
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever a business method is called on a session bean, the container creates a transaction for you if no transaction exists or joins the parent transaction when a transaction exists. This is the default behaviour unless you declare a specific transaction attribute type in the method level.

So if methodA has a default transaction attribute type, and it invokes methodB, methodB will inherit the transaction from methodA. If methodB fails, then methodA also fails.
You can play around with the transaction attribute types to control the transaction for each method.

I hope I answered your question.

Regards.

 
T Stevens
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply. I'm quite happy with the basic concepts, the bit that is confusing me is that it is possible to open and close multiple JDBC connections - I'd like to know how the transactions are managed at the database level.
Example:


In this example, businessMethodA calls businessMethodB and businessMethodC, both of which request and use a JDBC connection. My question is, how is this managed as a single database transaction, because to the developer it appears not to be.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!