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.
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
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.
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?
This tiny ad will self destruct in five seconds.
Free, earth friendly heat - from the CodeRanch trailboss