File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB and other Java EE Technologies and the fly likes EJB Transaction confusion 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 "EJB Transaction confusion" Watch "EJB Transaction confusion" New topic

EJB Transaction confusion

Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
Now I am confusing the EJB transaction. Too much contents there. Anybody can give me some concise rules when using the 6 transaction attributes in real world EJB programming? Usually in what situaction we should use which attribute ?
Lasse Koskela

Joined: Jan 23, 2002
Posts: 11962
Ask yourself some questions:
1) Whether the operation "must-have", "can-have", or "should-not-have" a transactional context?
This limits your choices to {Required, RequiresNew, Mandatory}, {Supported}, and {NotSupported, Never}, respectively.
From these sets, only the first one ("must-have") is left to resolve because, as a thumb rule, you should avoid using Never at all times.
2) If the operation fails, should *everything* rollback or just everything done within that particular operation?
If just the changes internal to the operation should rollback, use RequiresNew. If all changes should rollback, proceed to question 3.
3) Does it actually matter whether the transaction already exists?
If it does for some reason (can't really think of one), use Mandatory. If you don't care whether the transaction already exists or whether it's created just for this operation, use Required.

Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
vinay Yalamanchili

Joined: Dec 28, 2003
Posts: 1
Good description..thanks..
I agree. Here's the link:
subject: EJB Transaction confusion
It's not a secret anymore!