• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A transaction doubt

 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 446
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 446
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic