Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in stateful session bean

 
Shanmugam Karthikeyan
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Is there anything called container managed session bean and bean managed session bean ?

If so, can anyone explain me the importance and which one is the default for session bean.
 
Greg Charles
Sheriff
Posts: 2989
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are container-managed transactions and bean-managed transactions for session beans. Container-managed persistence and bean-managed persistence are only for entity beans. For CMT vs. BMT, there is no default, but I'd say CMT is more common.
 
MI Mohammed
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to add, for more clearification. BMT vs CMT. It's just way
of classifying beans basesd on how they manage there transactiions.
For BMT, you are to the container that that YOU, the bean provider
is going to manage the bean transaction (tx) yourself. And For CMT, you get
telling the Container, Hey, please manage my bean transction for me, and
the Container reply HOW. You just say check my bean DD for instructions on how.
To add more, for BMT, you just have to use the javax.transaction.UserTransaction interface, while for CMT, you have
to use the setRollbackOnly() and getRollbackOnly() from EJBContext. eg

BMT

UserTransaction ut = EJBContext.getUserTransaction(); // get the tx
ut.begin() // start the tx
foo();
bar();
if(allIsFine){
ut.commit(); // commit tx
} else {
ut.rollback(); // rollback tx
}

CMT

It's more of declarative, it has only two methods that
you can use to manage your tx and they are handled on methods bases.

if (allAnyBad) {
EJBContext.setRollbackOnly(); // tx will never commit.
}
To check for tx status,

BMT

ut.getStatus();

CMT

EJBContext.getRollbackOnly();

Hope the difference is clear.
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

For BMT, you are to the container that that YOU, the bean provider
is going to manage the bean transaction (tx) yourself. And For CMT, you get telling the Container, Hey, please manage my bean transction for me, and the Container reply HOW.

This is not correct. The Container always manages the transaction. The difference between BMT and CMT is about demarcation. For BMT, the enterprise bean transaction demarcation is done programmatically in the business methods, for CMT the transaction demarcation is done by the Container based on the transaction attributes in the deployment descriptor.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic