From documentation :
retainIfException: If true, the stateful session bean will not be removed if an exception is thrown from the designated method.
At the end of the lifecycle, the client invokes a method annotated @Remove
, and the EJB container calls the method annotated @PreDestroy
, if any. The bean’s instance is then ready for garbage collection.
Therefore, in case of exception, the bean isn't removed by the container. You can call this method to make it eligible for garbage collection.
@Remove(retainIfException=false)
public void remove() {
log.info("loginManager removed");
}
Edit:
If the application calls the remove()
on the instance of a session bean with scope other than @Dependent
it would result in UnsupportedOperationException
.
Stateful session beans may define a remove method, annotated @Remove,
that is used by the application to indicate that an instance should be
destroyed. However, for a contextual instance of the bean—an instance
under the control of CDI—this method may only be called by the
application if the bean has scope @Dependent. For beans with other
scopes, the application must let the container destroy the bean.