1. Typically you would not be creating Connection objects in the Servlet or Web tier classes.
2. Also to make use to Container managed transactions (CMT) you should never be calling Commit() on the Connection object
3. In CMT, the commit is automatically called by the EJB container based on the Transaction attribute defined in the deployment descriptor.
4. Yes this code will definitely throw an Exception. The transaction attribute is Mandatory and therefore the method needs to be called in the context of an exiting transaction context initiated by the EJB Container.
Hope this helps, Deepak
Joined: Mar 23, 2001
After some thoughts, I have some new understanding regarding this questioin.
1. One can feel free to create a Connection(as showed in the above code) and commit it afterwards in the Servlet.
2. Whether the transaction context propagate to the EJBs or not depends on how the transaction context is established. if it's done by simply acquiring the connection via JNDI, then the transaction context does propagate, while if it's done by calling the appropriate JTA method, more specificlly, javax.transaction.UserTransaction.begin(), the transaction context would definitely propagate to the EJBs.
3. In the above code, if the bean's transaction attribute is required or requirednew, the code would run successfully. there would be 2 seperate transactions.
4. If the code enclosing the line bean1.dosth() be replaced with
javax.transaction.UserTransaction.begin() and javax.transaction.UserTransaction.commit()
the code would also run successfully and there would be only 1 transaction.
All these are drawn by my one understanding. different ideas are always welcome.
Regards, James [ February 27, 2005: Message edited by: James Du ]
Joined: Feb 13, 2004
If you plan on designing the "Client Agnostic" application, which support more than 1 client types then things establishing connections etc should be done in the business tier.
The responsibility of initiating the transaction context should remain in the business components.
Ideally the web tier should never be involved in controlling the transactions.
Joined: Mar 23, 2001
I am here more on studying the EJB contract and limitation than planing a robust real world system. Just want to have a solid understanding about what could happen in a specific conditions.