File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Question regarding Lifecycle of STATELESS session bean. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Question regarding Lifecycle of STATELESS session bean." Watch "Question regarding Lifecycle of STATELESS session bean." New topic
Author

Question regarding Lifecycle of STATELESS session bean.

satish shankaran
Greenhorn

Joined: Aug 05, 2002
Posts: 3
The EJB specification states that:

A stateless session bean instance�s life starts when the container invokes newInstance()on the session bean class to create a new instance. Next, the container calls setSession-Context() followed by ejbCreate() on the instance.
The container can perform the instance creation at any time�there is no relationship to a client�s invocation of the create() method.

Can somebody help me to understand about

"The container can perform the instance creation at any time�there is no relationship to a client�s invocation of the create() method."

I was under the impression that there is a relation between instance creation and the client�s invocation of the create() method.

Thanks for your help.
Srini Madala
Greenhorn

Joined: Feb 27, 2005
Posts: 16
Remember that stateless session beans are not tied to any specific client. The same stateless session bean can be served to multiple clients during its life period. Stateless beans go back to pool once it serves client and are ready to accept requests from other clients. But the stateful session beans are tied to a specific client. So it is created when the client calls the create method on stateful session bean home interface and will end once it served the client.

So, think about it. Why do you need to create stateless session beans when client calls create method if these beans can serve multiple clients. The container may decide to create a bunch of stateless beans during server startup for performance.

When client calls create method on a stateless session bean, the container creates EJBObject and associates it with the bean.

Hope this helps....


Srini Madala<br /> <br />SCJP2, SCWCD1.3, SCBCD1.3
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1180
Thank you Srini for your explanation.

About:

The container can perform the instance creation at any time�there is no relationship to a client's invocation of the create() method.


Srini said:

When client calls create method on a stateless session bean, the container creates EJBObject and associates it with the bean.


I guess the other order of events might also be possible:

When client calls create method on a stateless session bean, the container creates EJBObject, then creates the bean and associates the bean with the EJBObject.

-- Dan


William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
Srini Madala
Greenhorn

Joined: Feb 27, 2005
Posts: 16
Yes Dan.. You are right. The container may create EJBObject first and then the actual bean by calling new instance(), setSessiionContext(), and then ejbCreate().

As far as stateless session beans are concerned, it is up to the container, how it creates the beans. So we can not for certain assume that the bean will be created only when client calls create method on its home itnerface..
Sen nathan
Greenhorn

Joined: Mar 27, 2005
Posts: 3
I understand that the stateless session bean instance creation is not tied to any specific client.But I have a doubt here, Let's say, the container precreates the instances by running constructor,context and ejbCreate.
When it completes the ejbCreate method there won't be any EJBobject as the client wouldn't have invoked the create method in home interface.In that case, how it is possible to get a reference to EJBobject from ejbCreate method.Both in spec as well as in Certification book, it is mentioned that EJBobject reference can be retrieved thru context API from ejbCreate method.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Question regarding Lifecycle of STATELESS session bean.