ejbRemove() doesn’t always get called, at least on WebSphere 5.0. Well… it’s shocking truth to me, but probably to none of you guys, and I’m not sure that it even should. I don’t know why I assumed that it did, it just seemed that it would given that it’s a lifecycle method.
I’ve been using a strategy where I register a JMX object in ejbCreate() and unregister it in ejbRemove(). Seemed pretty reasonable thing to do…. until I found all these lingering JMX objects without matching EJBs. Hmmm… A little bit of googling and there you go:
_
A session bean can miss a call to ejbRemove() if there is a container crash or the bean throws a runtime exception. Source: HF-EJB 221 (HF refers to Head First EJB by Bert Bates and Kathy Sierra)
_
Bugger. Now I wrap the body of each exposed interface method with a catch block for Runtime Exceptions, release my resources, then rethrow the exception. I ain’t pretty, but it’s more reliable.