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?
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.
Uy Jerwin Louise Vergara
Junior Developer / Research and Development at Incuventure Partners Corporation
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.
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.