File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB and other Java EE Technologies and the fly likes Deleting EJB from container cache? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Deleting EJB from container cache?" Watch "Deleting EJB from container cache?" New topic

Deleting EJB from container cache?

J Norm

Joined: Feb 24, 2003
Posts: 6
I'm looking for a way to force an EJB to be removed from the container cache. Or, at least a way to force the container to create a new EJB instance.
Here is the situation. I have a stateless session bean that connects to and communicates with a socket service. It is important that the EJB maintain a connection with the socket throughout it's life, but because of some bugs in the socket service itself, the EJB needs to reinitialize the socket connection from time to time.
What I'm supposed to do is write a delegate that uses the EJB, and when the delegate catches the exception indicating a corrupted socket connection, we want it to discard the EJB and get a new one. The problem is that when we discard the EJB, it goes back into the cache, and so when we ask for a new one, we are just getting the same EJB with the same bad socket connection. I'm currently using the remove() method from the EJBObject interface, but all that seems to do is place the EJB back in the pool.
Is there a way to explicitly destroy an EJB instance?
Lasse Koskela

Joined: Jan 23, 2002
Posts: 11962
One way would be to force a system exception being thrown:

Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Dana Hanna
Ranch Hand

Joined: Feb 28, 2003
Posts: 227
This should not be EJB - it should use the singleton pattern. It can be wrapped in a session bean and deployed to the server, but as a seperate component. This singleton could have an invalidate method indicating to destroy and re-create the socket connection.
I agree. Here's the link:
subject: Deleting EJB from container cache?
It's not a secret anymore!