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 ]
posted 16 years ago
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 ?
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?
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 ]
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
posted 16 years ago
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?
You have to be odd to be #1 - Seuss. An odd little ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop