wood burning stoves 2.0*
The moose likes Architect Certification (SCEA/OCMJEA) and the fly likes A transaction doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Architect Certification (SCEA/OCMJEA)
Bookmark "A transaction doubt" Watch "A transaction doubt" New topic
Author

A transaction doubt

James Du
Ranch Hand

Joined: Mar 23, 2001
Posts: 186
Hi, all

Please take a look at the following code, f1() is a method of a servlet.


Do you think bean1.doSth() will throw an Exception? Can we consider that the method is called with a transaction context?


Regards,
James
Deepak Pant
Ranch Hand

Joined: Feb 13, 2004
Posts: 443
Hi James,

Couple of key points:

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
James Du
Ranch Hand

Joined: Mar 23, 2001
Posts: 186
Thanks Deepak,

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 ]
Deepak Pant
Ranch Hand

Joined: Feb 13, 2004
Posts: 443
James,

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.

regards,
Deepak
James Du
Ranch Hand

Joined: Mar 23, 2001
Posts: 186
Thanks Deepak

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.

regards,
James
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: A transaction doubt
 
Similar Threads
setRollbackOnly
Entity Bean - BMP - Transaction
Transaction Management in EJB
does the stub depend on the client location?
ejbCreate -permitted and prohibited operations -