Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MDB-concurrency , serializing and reentrant?

 
Prashant Neginahal
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,

I come across following statement in EJB spec for Message Driven bean(p:316).
-------------------------------------------------------------------------
Most containers will support many instances of a message-driven bean executing concurrently; however, each instance sees only a serialized
sequence of method calls. Therefore, a message-driven bean does not have to be coded as reentrant.
--------------------------------------------------------------------------

I have 3 questions seeking more clarification.

1) What is concurrent execution in EJB? Is it executing bean instances at same time?
2) What is serializing the method calls?
3) Finally, what is reentrant with respect to concurrency and serializing?

Thanks,
Prashant
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generally speaking, a container will deal with each request by pulling a thread from the thread pool and assigning it to that request. If two requests arrive at the same time and both need to be serviced by the same bean type, the container will pull two instances from the pool, each will run in a separate thread. If no more instances are available, and the pool size cannot be increased, then the container may serialize (queue) the requests until an instance is available.

Note that this scenario cannot apply to stateful session beans as they are not pooled. If memory serves me right, as I can't be bothered to look it up, the second client will get an exception thrown by the container.
 
Prashant Neginahal
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Roger. Can you please explain me the concept of re-entrant with an example?

Thanks,
Prashant
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only entity beans are permitted - though discouraged - to make reentrant calls. Session beans and MDBs cannot make reentrant calls.

Conceptually, reentrant means that bean 1 calls bean 2 which calls bean 1. So, you might expect a call to bean 1 on the stack, then bean 1's call to bean 2, then bean 2's call back to a bean 1 method would be on top. This looks fine as we have just a single thread stack of execution for the transaction.

But in the EJB world, all bean calls go via the EJBObject, with the Container managing calls by spawning multiple threads for them. There is no way for the Container to tell the difference between a genuine reentrant call to bean 1 and a call coming in from another client. So, thread-safety will be compromised if you tell the Container to allow that call from another client in the mistaken belief that it a genuine reentrant call.

The bottom line is this: never use reentrant calls for entity beans unless essential and you know what you are doing.
 
Prashant Neginahal
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot,Roger.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic