File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes BMT with Entity beans Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "BMT with Entity beans" Watch "BMT with Entity beans" New topic
Author

BMT with Entity beans

Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643
what is reason behind Entity beans cannot use BMT?
Aakash Parashar
Ranch Hand

Joined: Jul 25, 2005
Posts: 118

Entity beans can use bean managed transactions when they are coded with BMP (bean manged persistence). CMP Beans can't use bean managed transactions.


Your's Aakash
SCJP 1.4 96%, SCBCD 1.3 94%
http://java-application-programming.blogspot.in
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Entity beans can use bean managed transactions when they are coded with BMP

No, entity beans cannot.


[My Blog]
All roads lead to JavaRanch
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643
Now i am more confuse.Can anyone pass me a link so that i will be able to understand why Entity beans can't use CMT.
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643
Sorry for duplication.
Now i am more confuse.Can anyone pass me a link so that i will be able to understand why Entity beans can't use BMT.
Celinio Fernandes
Ranch Hand

Joined: Jun 28, 2003
Posts: 547

that is a good question in my opinion. I did some research and found this :

Entity beans aren't permitted to use bean-managed transactions (presumably to encourage reuse of entity beans). In fact, the EJB specification states that any transaction in force when a BMT method is called should be suspended by the EJB container before the business logic is invoked. Thus, using BMT beans in various transactional contexts isn't possible.
Bean reuse is really maximized by using container-managed transactions.

But I would like to have more explanation.


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCBCD 5
Visit my blog
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643
In Mastering EJB book i have found following but i am not able to understand
it.

Perhaps one of the most misunderstood concepts in EJB is how transactions relate to entity beans. Let�s explore this concept.
When you call an entity bean in a transaction, the first action that happens is the entity bean needs to load database data through the ejbLoad() method,
which acquires locks in the database and ensures the entity bean cache is
consistent. Then one or more business methods are called. When the
transaction is committed, the entity bean�s ejbStore() method is called, which writes all updates to the database and releases the locks. A transaction should thus span the ejbLoad() business methods, and the final ejbStore(), so that if any one of those operations fail, they all fail.
If we were to use bean-managed transactions, we would write code to
perform begin() and commit() methods inside our bean (perhaps around the
JDBC code). Perhaps we would start the transaction in ejbLoad(), and then
commit the transaction in ejbStore(). The problem, though, is that you do not call your own ejbLoad() or ejbStore() methods�the container does. The bean cannot enforce that these methods happen in this order, if at all. Therefore if you started a transaction in ejbLoad(), the transaction may never complete.
Because of this, bean-managed transactions are illegal for entity beans.
Entity beans must use declarative transactions. Session beans or messagedriven
beans can use bean-managed transactions because a session bean can
load database data, perform operations on that data, and then store that data; all in a single method call, and thus is in direct control over the transaction.
A corollary of this discussion is that entity beans do not load and store their data on every method call; rather, they load and store their data on every transaction. If your entity beans are not performing well, it could be because a transaction is happening on each method call, and thus a database read/write is happening on every get/set method. The solution is to make sure your transactions begin earlier and end later, perhaps encompassing many entity bean method calls. By properly controlling the duration of your transactions with transaction attributes (as we will see later in this chapter), you can control when database reads and writes happen with entity beans.
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643
Any through?
Sanjeev BhimaRao
Ranch Hand

Joined: Jul 17, 2006
Posts: 71
I am not sure if this is entirely accurate, but these are my thoughts.

Entity Beans exist because there is an entity in the DB that they represent. CMP fields representing the complete entity are declared in the DD, along with abstract getters and setters.

On the other hand, with BMT, the bean manages DB access code by itself upon notification from the container. That means, we could do DB access code or do nothing in the BMT container callback methods. But with CMT, container is in charge of putting the required code for DB access.

This makes me think that a BMT bean is not tied to any real entity. And if its not tied to any real entity, it cannot be an entity bean.

Another point to note is that if a session bean uses an entity bean and if the session bean is in a transaction and wants the entity bean (DB) updates to happen as a part of the same transaction, only CMT would support it. With BMT, the entity bean methods would run in a seperate transaction. So, a scenario like "Insert New Customer through a session bean which uses entity bean" will run into issues.

Not sure if I made sense. Correct me if I am wrong.
 
jQuery in Action, 2nd edition
 
subject: BMT with Entity beans
 
Similar Threads
Entity Beans and Bean Managed Transactions
BMT Beans
Why entity beans cannot be coded as BMT?
Why bean managed transaction is not allowed with entity beans
About BMT