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

Entity Beans and Bean Managed Transactions

 
Joseph Jegan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vishwa Kumba
Ranch Hand
Posts: 1066
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why isn't BMT allowed for BMP code in Entity beans?
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1066
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why JTA calls inside ejbLoad() and ejbStore() would result in an endless loop?

Thanks,
Guru
 
Valentin Tanase
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dave Clark
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic