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

Doubt of instances pool

 
Lee Ming
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the bean instances of a stateless bean or message driven bean are the same to all clients, why needs a pool of bean instances(not only one instance) ?
thanks to your help !
 
Philippe Maquet
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lee,
Welcome to JavaRanch !
If the bean instances of a stateless bean or message driven bean are the same to all clients, why needs a pool of bean instances(not only one instance) ?

For a given bean class, to let the container serve multiple clients concurrently.
Best,
Phil.
[ December 15, 2003: Message edited by: Philippe Maquet ]
 
Lee Ming
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Philippe,
thanks for your reply, but my doubt is that I think the container uses multiple threads to serve multiple clients concurrently, is that right?
If it is right, so why the threads serve the clients do not use one same instance? I knows that if a bean instance is not thread save, then the container should use multiple bean instances to serve clients, is a bean instance of statless bean or message driven bean thread save ?
 
Ashish Pagare
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the container uses multiple threads to serve multiple clients concurrently, is that right?

Yes
If it is right, so why the threads serve the clients do not use one same instance?

Because all the calls made by container on stateless and message driven beans are serialized i.e. no 2 threads will be executing same method of one particular bean instance and thus to support concurrency container manitains pool of bean instances.
Is a bean instance of statless bean or message driven bean thread save ?

No, as explained above.
Hope this helps. Please refer to spec for more details.
Ashish
[ December 15, 2003: Message edited by: Ashish Pagare ]
 
Nandu Jawale
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To answer the question, a bean instance is MAY NOT be the same for all clients of a stateless session bean. You never know whether you are calling method on the same bean instance you were talking to. The container might have got a new instance from the pool to serve your next request.
Consider this scenario:
1. Container has 3 bean instances in a pool, A, B and C.
2. Client 1 makes a method invocation, which is served by A.
3. Client 2 makes a method invocation, which is served by B.
4. Meanwhile container might moved A back to pool.
5. Next time client 1 makes a method invocation, container may choose C to serve this request.
Having a pool of bean instances can facilitate multiple clients calling methods on EJB at the same time. Cocunrrency is different issue.
Hope it helps.
Regards,
Nandu
 
Lee Ming
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all of you!
Now I know the fact is that the container must ensure that only one thread can be executing an instance at any time(spec 7.11.8 & 15.8.3).
But I have another question, why does the EJB spec give such restriction to message driven bean and stateless session bean? I ask this question since I think the instances(from the same home if it have) of a bean class(message driven or stateless session) are equivalent to all clients! Is that means the states of such instaces are not the same to the container durning a business method invocation?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic