4.3.2 Precautions during the implementation of a Stateful Session Bean
(1) Deleting the EJB instances and releasing the references with either the remove method or a method in which the @Remove annotation is specified
- When you invoke a Stateful Session Bean using the home interface, you acquire the references by invoking the create method of the home interface. However, when the invocation of the Session Bean is complete, you must delete the EJB instances and release the references by invoking the remove method.
- When you invoke a Stateful Session Bean using the business interface, once the invocation of the business method is complete, you must delete the EJB instances and release the references by invoking the method in which the @Remove annotation is specified.
- If you do not delete the EJB instances and release the references, the memory on the J2EE server will remain occupied.
(2) Precautions about sharing of Bean classes
Do not use the same Session Bean as a Stateful Session Bean and Stateless Session Bean in the same J2EE application.
(3) Precautions concerning destruction of SessionSynchronization instances
If a system exception occurs during the beforeCompletion and afterCompletion method of SessionSynchronization, the instances of the corresponding Session Bean will not be destroyed in the EJB container.
(4) About invocation of the begin method of javax.transaction.UserTransaction with the setSessionContext method
The setSessionContext method of the Stateful Session Bean is used for invoking the begin method of javax.transaction.UserTransaction. However, according to the EJB specifications, you cannot use this method after invocation. Do not invoke the begin method of javax.transaction.UserTransaction.
(5) About invocation of an Enterprise Bean from the afterCompletion method
When you invoke another Enterprise Bean from the afterCompletion method of the Stateful Session Bean, the following operation will be performed depending on the operation mode of the J2EE server mode:
- 1.4 mode: The Enterprise Bean is invoked.
According to the EJB specifications, you cannot invoke another Enterprise Bean from the afterCompletion method of the Stateful Session Bean. Do not invoke another Enterprise Bean.