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 Why Entity beans are always CMT 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 "Why Entity beans are always CMT " Watch "Why Entity beans are always CMT " New topic

Why Entity beans are always CMT

Kartik Patel
Ranch Hand

Joined: Sep 12, 2005
Posts: 73

I went thru spec and many other resources to find that why entity bean should always run in container managed transaction.???

But i only got answer that it should be because its written in the spec...
but can someone tell me valid reason of having just CMT for entity bean..

Session bean can run in both BMT and CMT...

So why not entity bean???

Chop your own wood, and it will warm you twice. - Henry Ford
Shrinivas Mujumdar
Ranch Hand

Joined: Aug 27, 2004
Posts: 328
Hey Kartik,
Entity beans(CMP & BMP) provides objectified representation of data in underlying datastore, they are always transactional meaning,whenever you invoke any method on them its always executed within Transaction.
Main idea of having entity bean is DATA PERSISTANCE & DATA MANIPULATION through java objects, hiding the complexity of DB Operations like queries,Transactions etc...
Session Beans however are transaction aware, they represent process / workflow.

I hope this helps, otherwise Ping Javaranch one more time.

I would suggest Read Topic from Oreilly(Richard Manson) & Head First EJB

Shrinivas Mujumdar

Kartik Patel
Ranch Hand

Joined: Sep 12, 2005
Posts: 73
Hey Shrini,
Thanks for prompt reply.. I got ur point that entity beans are only meant for Transactional processes... and in session bean u can have transactional process as well as non-transactional processes...

but even though my question remain same... if transaction are associated with the entitt beans than why cant we use BMT???

So we can reduce the scope of the transaction
if Sun doesn't want to disturb the lifecycle of entity bean than it could have given BMT entity bean with restriction that one can reduce the scope of transaction but cant expand it to more than 1 method....So its like special kind of bean managed transaction...

Rather it would be a good idea to provide this extra feature to enity bean as the are always involved with transaction.... and sometimes u just want a part of the method to be in transaction .. not just the whole method... and sometimes u want to put more than one transaction in just 1 method... so in this case BMT entity bean could be a good idea...

but Why does SUN Microsys. not provided this feature of BMT in entity bean...??
Nadeem Awad
Ranch Hand

Joined: Jan 09, 2005
Posts: 74
Originally posted by Kartik Patel:
but can someone tell me valid reason of having just CMT for entity bean..

Dear Kartik,
The purpose of the entity bean is to represent some data on the persistence store, provided that the container is responsible for synchronizing the entity instance's state with that store. for example, if a client is calling a business method on the entity bean, the container calls ejbLoad() before the execution of the method, and calls ejbStore() after the method execution has finished and before the transaction is commited. So, If you want the entity bean to be BMT, then you should handle the entity's state synchronization by calling ejbLoad() and ejbStore() along with transaction control statements (i.e. begin, commit). In that case, you are defeating one of the major services that the EJB container provides and entity beans exist for, which is providing a simple OO view of some entities stored in the persistent storage. That said, if you need to implement these services yourself, then you sould consider writing a POJO to access the persistence store.

Best regards,
[ December 14, 2005: Message edited by: Nadeem Awad ]

Share Knowledge to gain it.<br /> <br />SCJP 1.2, SCDJWS 1.4, SCWCD 1.4, SCBCD 1.3, ICAD
Valentin Tanase
Ranch Hand

Joined: Feb 17, 2005
Posts: 704
Hi Kartik,

This thread might clarify your doubts even further.


I think, therefore I exist -- Rene Descartes
I agree. Here's the link:
subject: Why Entity beans are always CMT
It's not a secret anymore!