unsetSessionContext

Discussions

EJB programming & troubleshooting: unsetSessionContext

  1. unsetSessionContext (3 messages)

    why is the unsetsessionContext API absent in interface SessionBean. Interface EntityBean defines set/unsetEntityContext() API. they why unsetsessionContext () API is not there?

    Regards,
    Rashmi

    Threaded Messages (3)

  2. unsetSessionContext[ Go to top ]

    Rashmi:

      entity beans are related to out of container resources. The basic existance of entitybeans is dependend of the persistance layer. Hence unsetEntityContext is must functionality. so as to detach an instance from external resource or maintain pool size. (I guess pool size maintanance is added advantage with unsetEntityContext). Main reason is to release external resources.

    I have put some of the EE specs below.


    1. public void unsetEntityContext(); A container invokes this method before terminating the life of the instance. This method executes with an unspecified transaction context. An identity of an entity object is not available during this method. The entity bean must not attempt to access its persistent state
    and relationships using the accessor methods during this method. The instance can take advantage of the unsetEntityContext() method to free any resources that are held by the instance. (These resources typically had been allocated by the
    setEntityContext() method.)

    2. public void unsetEntityContext(); The container invokes this method when the container wants to reduce the number of instances in the pool. After this method completes, the container must not reuse this instance.The container invokes this method with an unspecified transaction context.

    3. An instance in the pool can be removed by calling the unsetEntityContext() method on the instance.

    Hope this helps
    Regards
    Chetan
  3. unsetSessionContext[ Go to top ]

    thanks a lot chetan, it helped me a lot...
  4. Clarification in the matter[ Go to top ]

    Since this thread is easy to find from google (first hit), a clarification might be in place.

    The unsetEntityContext is only needed for entity beans since these have an extra state. Basically these have the states “does not exist”, “pooled” and “ready”. The session beans do not have the pooled state (they are pooled however). unsetSessionContext is called when the container decides to evict an instance from the pool to the does not exist state (it is garbage collected).

    The unsetSessionContext can be used to clean up resources which a pooled bean needs e.g. a connection to a database. Remember that the resources must not be related to a specific instance since the bean is not tied to a specific instance in the pooled state.

    In the most common case, the resource is related to a specific instance, in these cases the clean up should be in the ejbRemove and ejbPassivate methods.

    All beans can have external resouces and the resouces must be managed differently depending on the bean type. Session beans are often related to external resources (e.g. database connections).

    /Erik