File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes IS it advisable to use Transaction suppourt from servlets of with in EJB Bean class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "IS it advisable to use Transaction suppourt from servlets of with in EJB Bean class" Watch "IS it advisable to use Transaction suppourt from servlets of with in EJB Bean class" New topic
Author

IS it advisable to use Transaction suppourt from servlets of with in EJB Bean class

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234


Hi

I am using servlets and EJB 3 to perform a insert operation .

Please let me know is it good to a have transaction suppourt inside the servlet (with the help of USerTransaction ) or
inside the EJB3 bean method (TransactionAttrribute on top of it )

Thanks in advance .

Save India From Corruption - Anna Hazare.
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

It's difficult to tell without knowing the context. Basically if there a need that cannot be achieved by having the transaction scope limited to work done from within one (or more) ejb (methods), then UserTransaction is the only way to go - for all others use CMT. At least would be my approach.

cheers,
ram.
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Thank you Ramprasad for your time .

Can you please elobrate a little on this .

Basically if there a need that cannot be achieved by having the transaction scope limited to work done from within one (or more) ejb (methods), then UserTransaction is the only way to go


Thanks in advance .
Manuel Alberto Quero
Ranch Hand

Joined: Jul 17, 2009
Posts: 31
Basically, what you need to know is if you need client-initiated transactions or not.

-You need client-initiated transactions if you need to keep a transaction between calls to ejb methods (assuming you are just using stateless session beans). In this case, you will need to initiate the transaction from the servlet by using the UserTransaction interface.

//client-side (servlet)
...
try
{
Context initialContext = new InitialContext();
UserTransaction utx = (javax.transaction.UserTransaction) initialContext.lookup("UserTransaction");
utx.begin();

//call ejb method1 (avoid REQUIRES_NEW transaction attribute)

//call ejb method2 (whithin the same transaction) (avoid REQUIRES_NEW transaction attribute)

if (success)
utx.commit();
else
utx.rollback();
}
catch (Exception e)
{
...
}
...

-If you donĀ“t need to keep the transaction between ejb method calls, then do not use client-initiated transaction (i.e. don't need to use UseTransaction interface as long as you use CMT)

Hope this helps,

Manuel
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Hey Thank you very much for your time .Thats great explanation .
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

Actually I meant slightly otherwise. Even calls that involve multiple ejb calls may be wrapped in a outer ejb (the popular session facade pattern) where the outer ejb which is coarse grained can have a 'requiresnew' and the fine grained ejbs (which do the actual work) can have a 'required' attribute for transaction. Consider a travel site where in a single page you can make multiple reservations (say one for a flight, another for cab services and yet another that does hotel reservations). You will have individual ejbs that do each of the work. And then you have an outer ejb (coarse grained) which invokes these other ejbs in one method. There may be a case when these ejbs have to be invoked individually (like when the user makes only one of the three possible bookings) and you can still use CMT with them (remember they have a 'required' attribute). If you need nested transactions you can even have a requiresnew on each of the ejbs (this depends on your server implementation too).

That said, you can make a UT for the above scenario and it generally depends on the exact nature of your business requirements. And while there are best practises and everyone has a chosen way that they lean towards, I believe any design that is scalable, robust and maintainable is good design irrespective of the technology or the tools used. I favor container services wherever I can leverage it - after all it is well tested and written by people who understand the nuances better and are SMEs in the field. So my choice for the above implementation would be CMT.

Now for the UT part - you may sometimes have a need for an atomic operation where you have to make an ejb call and then say a web service call. There are somethings which naturally lend themselves to a servlet program model than an ejb and web services where you make a http call over the n/w is one of these (mind you - it maybe possible to make a ws call from an ejb too which is something I have never attempted though). So if you chose (like me) to make the web service call from your web layer and then an ejb call (to the dao layer perhaps) and these calls need to be in the scope of a transaction (as defined by your business requirement), then you have a case for UT.

Does all that make sense?

ram.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: IS it advisable to use Transaction suppourt from servlets of with in EJB Bean class