Source: ETS (Enthuware)
Which of the following methods can be used by a stateful session bean to clean up the resources that it has
acquired before its removal?
Options:
1) finalize -> Incorrect because session beans must not have finalize() according to Spec. However its a different story that when i had one,
JBOSS did not bother to complain.
2) @Remove method -> This is the answer acc to ETS. And i dont agree with this because.
a) Invoking Remove method is client dependent, If the client does not invoke remove method, then the instance will be passivated and finally timedout. In this case
PreDestroy method will not be invoked. Hence PreDestroy() method is also not the correct place to release resources.
So how can this be correct answer ETS.
3) afterCompletion: This can be used only if sfsb is using CMT. However if used then the resources will have
to be acquired again before the start of next new transaction. Hence this is also not a reliable method. It never
works for BMT anyway. Since tje question does not metion of the type of transaction model, this option cannot be
chosen. Even if CMT was used and the resources acquired in NotSupported, Supports and Never business methods cannot
be released in afterCompletion, cause afterCompletion will not be invoked. Hence this option is ruled out.
4) unsetSessionContext: This method does not exists.
Hence i think there is no answer for this question for the options that are given.
ETS Answer and explanation:
Remember:
@Remove method
An
ejb acquires resources in PostCreate and PostActivate callback methods and it releases the same resources
in Remove and PrePassivate methods.
If a bean is passivated and if the timeout occurs, the container does not call the Remove method.
I think the answer to this question is PrePassivate() and REMOVE method both together. Only PrePassivate() is
guaranteed to be invoked in all circumstances before a instance moves from methodReadyState and DoesNotExists.
However if the client invokes Remove method then PrePassivate() will also not be invoked.
Hence the correct answer is have resource removal code in Remove "AND" PrePassivate methods. Remember its AND
and NoT OR.
Is my understanding correct?