aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Entity Beans and Bean Managed Transactions 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 "Entity Beans and Bean Managed Transactions" Watch "Entity Beans and Bean Managed Transactions" New topic
Author

Entity Beans and Bean Managed Transactions

Joseph Jegan
Greenhorn

Joined: Sep 06, 2000
Posts: 10
All,
The EJB spec says, Bean Managed Transaction is not possible in Entity Beans (Both BMP and CMP). Can some one explain me the reason?
Thanks in Advance
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
Ok, think this through. The accessors and mutators of Entity beans are automatically generated by the container from abstract methods in your class -- you don't code them. Thus if you were to try to put in BMT code, where would it go?
Kyle


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
Vishwa Kumba
Ranch Hand

Joined: Aug 27, 2003
Posts: 1066
Why isn't BMT allowed for BMP code in Entity beans?
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
Again, think it through. ejbLoad() and ejbStore() are called through the JTA mechanism. Where would you put the calls to JTA to make this make sense? It'd be too easy for people to try to put the JTA calls INSIDE ejbLoad() and ejbStore(), which would result in an endless loop.
You could try to restrict yourself to putting them in the accessors and mutators, but then again that's a bad idea too -- miss one and you're hosed.
All in all, CMT is a much better idea where Entities are concerned.
Kyle
Vishwa Kumba
Ranch Hand

Joined: Aug 27, 2003
Posts: 1066
thanks. It looks like the entity beans were not designed to be used with BMT at all.
One of the things that often comes to mind for developers like me is to visualize a method in the BMP entity bean containing lengthy business logic with BMT transactions used inside the method for some lines of code.
I guess such code should be moved to session beans and entity beans are meant to act as pure persistence-object wrappers for the relational table(s) in the database.
[ April 19, 2004: Message edited by: Vishwa Kumba ]
Gurumurthy Ramamurthy
Ranch Hand

Joined: Feb 13, 2003
Posts: 272
Why JTA calls inside ejbLoad() and ejbStore() would result in an endless loop?

Thanks,
Guru
Valentin Tanase
Ranch Hand

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

The answer is not very intuitive, or at least it was not for me. There are several reasons that might make this choice very legitimate, starting from a design standpoint, arguing that entity beans are an OO view of the data; therefore they might know nothing about the transactions surrounding them. Another reason may also be the fact that if entity beans will be allowed to demarcate transaction boundaries in an arbitrary way, then things will become far too complex (imagine bean reuse, caching pooling, etc within this context). There is one more scenario that I�d like you to consider: imagine that an entity bean can start/stop its own transaction. Now considering that entity beans are also re-entrant, it might be possible for a client to start a transaction, calling bean A, which calls bean B that calls bean A again. Now if A could independently start its own transaction you might face the situation where the client is initiating a nested transaction. But J2EE doesn�t support nested transaction either ...
Regards.


I think, therefore I exist -- Rene Descartes
Dave Clark
Ranch Hand

Joined: Feb 16, 2005
Posts: 52
of course there is a way you can use bean-managed transactions and entity beans - you can simply use the JTA APIs to demarcate the transaction boundaries within a session bean that then calls the entity bean(s), and passes it's transaction context.

...but I gues this wasn't the question ;-)

cheers,

Dave.


Dave Clark<br />Senior WebSphere Architect<br /><a href="http://www.versant.com" target="_blank" rel="nofollow">Versant Open Access - JDO2 & EJB3</a>
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Entity Beans and Bean Managed Transactions