The proposed design will have unpredictable behavior as life cycle methods are handled by the container. A session without regard to the state of a bean is combined by a container (in most cases), and the same instance can be served by several requests.
The ejbCreate()
and ejbRemove()
methods call the container when the bean is initialized for the first time and when it is removed from the pool, respectively. Therefore, it can open a connection in ejbCreate()
, but cannot close it and service requests with the same connection.
But, if the connection is open, and the bean remains in the idle pool, it will redirect the resource unnecessarily, it may result in exceptions such as socket timeout, too many open connections, etc.
It is better to write a generalization method for opening / closing a connection in order to properly use resources.
Edit: Of the core J2EE templates - Service Latitude
Use the Service Locator object to abstractly use JNDI and to hide the complexity of creating the source context, finding the source EJB, and re-creating the EJB. Multiple clients can reuse the Service Object Locator to reduce code complexity and provide a single point to monitor and improve performance by providing a caching facility.
source share