Hello, I deploy a stateless session beans and then call remove() on the remoteObject. But, the ejbRemove() is never called on a stateless session bean. Shouldn't the container call the ejbRemove() when I invoke remove?
As per my understanding, when a client calls remove on a Stateless SB, the client is just indicating that she/he is done witht he bean and will not be able to use the EJB Object reference anymore. The container throws an exception if you try to use the reference once you call remove. The container does not remove the bean and does not call ejbRemove when the client calls remove. The container removes the bean whenever it likes depending on its implementation.
To answer ur question simply, the ejbRemove is not called when client calls remove for a stateless session bean.
1.But the reason I raised this question is because, let's say I establish a database connection in ejbCreate. So, now in ejbRemove() I have written a code that will disconnect to the database. So, though the client says that he has finished using the bean, the container won't still call ejbRemove(), which means the databse connection stays.
And if I am going to connect and disconnect in business method itself it would be very slow.
2.If it varies from container to container, what does the spec say about this?
Joined: Oct 09, 2003
SLSB dies(does not exist anymore) at the Container's will or when the bean throws a system(unchecked/uncaught) exception.If the bean is removed by the container beacuse the pool size is too big or some other reason then ejbRemove() is called by the container before the bean dies. If the bean dies because of a system exception then ejbRemove() is not called and the system resources are not freed. As you said, ejbCreate and ejbRemove are the places to connect and disconnect to DB. Someone answer this --If a system exception is thrown, how do we free the resources?
If you want you bean to be scalable, you won't open a connection in ejbCreate. What happens if you have 100 beans in the pool? You would end up with 100 open database connections. The best way is to lookup the DataSource during ejbCreate and then use it to get a connection only when you need it within a business method and close it when you are done with it.
The database connection is not actually opened and closed if you do this, so getting the connection should not be that much overhead. At least not compared to the overhead of maintaining all those database connections.
Thats was a good point. Also as per the specifications, we can't depend on the remove() method either for stateless or stateful session beans. Developers have to ensure system still releases all resources evebn without getting to remove methods.