Can someone give more details of ejbActivate() and ejbPassivate() and where it is used in both the Bean managed and Container Managed Bean.
If too many beans are instantiated, the container can passivate some of them.ie write the bean to some temp storage. The container should release all resources held by the bean.
Just before passivating,the container calls the ejbPassivate() method.So release all resources here,ie,close socket connections..etc.
When a passiavted bean is called,its said to be activated.The container then calls the ejbActivate() method.Acquire all the required resources for the bean in this method.ie get socket connection...
The app server ur using cannot always instantiate as many beans as the number of active sessions, for the very reason that it is resource hungry. For eg, if there are 100 clients communicating with ur server, there will not always 100 session beans servicing the clients.. instead the container instantiates a reasonable number of bean instances, say 25, to service the 100 clients simultaneously.
Now, the interesting problem of managing the 100 clients with 25 bean instances is with the container. The ejbPassivate() and ejbActivate() methods give a helping hand to the Container to effectively manage the resources.
When a bean instance is not being used (most containers detect it using Least-Recently-Used algorithm) the container invokes the bean's ejbPassivate() method... (as a bean developer you are supposed to released bean specific resources such as Socket Connection, DBConnections, File resources, etc.) which gives you a chance to release the resources acquired by your bean.If your bean is stateful, the container will now serialize the state information held by ur bean.
ejbActivate() is supposed to be the vice-versa of ejbPassivate... Suppose a client invokes a bean method, but the container detects that a free instance is not available from the pool, so , now the container will passivate one of the idle beans, invokes the ejbActivate() method in the context of the new client's request (..as a developer u r supposed to re-acquire the resource u released in ejbPassivate() method). By now the container deserializes the state information and populates it to your bean... and executes the business method the client invoked...