Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When to use transaction on method level?

 
Kent Xu
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Does anyone have a good example when method level transaction is better than set it on bean level?
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by bean-level?
 
Eduard Manas
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean when would you use Container Managed Transactions/CMT(method level) or Bean Managed Transactions/BMT (bean level)?
Typically you will want to use CMT whenever you can, as it allows a cleaner separation between transaction control and business logics; the bean developer only focuses on business logics and you leave transaction details to the container (grab any EJB book, they'll give you a much better comparison)
However, as live itself, everything comes to a cost, and the benefit of CMT introduces a 'small' performance problem on where you may end up locking database resources for longer than necessary.
Sometimes this 'small' overhead is a problem. You may want to have a look at BMT.
Going back to your question, you would use BMT, for example, when you have a large method doing time consuming processing, and only a small part of it performs database access requiring transactional isolation. In that case, assuming that performance is an issue, you may not want to have a 'method transaction', but to just start and commit the transaction to wrap your database code only.

... however, if you meant something else by 'method transaction' or 'bean transaction', then the answer would be I don't know...
Eduard
 
Chris Mathews
Ranch Hand
Posts: 2712
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think he is talking about the level of granularity in declaring transaction attributes. Transaction attributes can be assigned to all methods in an EJB (what Kent refers to as "bean-level") or can be assigned to individual methods (what Kent refers to as "method-level").
Obviously, you use "bean-level" granularity when all the methods in an EJB have the same transactioning requirements.
Use "method-level" granularity when methods have different transactioning requirements.
Actually, you could use both. Define a "bean-level" transaction setting and override the setting on any methods with different requirements using "method-level" syntax.
 
Eduard Manas
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
um, interesting... curiosity is killing me here...
Kent, may I ask you what did you mean by 'bean level'?
Eduard
 
Kent Xu
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am talking about the granularity in declaring transaction attributes. It is quite obvious when to define transaction attributes on the entire bean, but I don't understand very well when would I want to define it for each method (maybe some of the methods).
I am digest Chris's answer.
Thanks,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic