EJB.6.4 Conversational State


The conversational state of a stateful session object is defined as the session bean instance's field values, plus the transitive closure of the objects from the instance's fields reached by following references to objects in the Java programming language.

In advanced cases, a session object's conversational state may contain open resources, such as open sockets and open database cursors . A container cannot retain such open resources when a session bean instance is passivated. A developer of such a session bean must close and open the resources in the ejbPassivate and ejbActivate notifications.

EJB.6.4.1 Instance Passivation and Conversational State

The bean provider is required to ensure that the ejbPassivate method leaves the instance fields ready to be serialized by the container. The objects that are assigned to the instance's non- transient fields after the ejbPassivate method completes must be one of the following:

  • A serializable object. [1]

    [1] Note that the Java programming language serialization protocol dynamically determines whether or not an object is serializable. This means that it is possible to serialize an object of a serializable subclass of a non-serializable declared field type.

  • A null .

  • An enterprise bean's remote interface reference, even if the stub class is not serializable.

  • An enterprise bean's home interface reference, even if the stub class is not serializable.

  • A reference to the SessionContext object, even if it is not serializable.

  • A reference to the environment naming context (that is, the java:comp/env JNDI context) or any of its subcontexts.

  • A reference to the UserTransaction interface.

  • An object that is not directly serializable, but becomes serializable by replacing the references to an enterprise bean's remote and home interfaces, the references to the SessionContext object, the references to the java:comp/env JNDI context and its subcontexts, and the references to the UserTransaction interface by serializable objects during the object's serialization.

This means, for example, that the bean provider must close all JDBC API connections in ejbPassivate and assign the instance's fields storing the connections to null .

The last bulleted item covers cases such as storing collections of remote interfaces in the conversational state.

The bean provider must assume that the content of transient fields may be lost between the ejbPassivate and ejbActivate notifications. Therefore, the bean provider should not store in a transient field a reference to any of the following objects: SessionContext object; environment JNDI naming context and any its subcontexts; home and remote interfaces; and the UserTransaction interface.

The restrictions on the use of transient fields ensure that containers can use Java programming language serialization during passivation and activation.

The following are the requirements for the container.

The container performs the Java programming language serialization (or its equivalent) of the instance's state after it invokes the ejbPassivate method on the instance.

The container must be able to properly save and restore the reference to the remote and home interfaces of the enterprise beans stored in the instance's state even if the classes that implement the object references are not serializable.

The container may use, for example, the object replacement technique that is part of the java.io. ObjectOutputStream and java.io.ObjectInputStream protocol to externalize the remote and home references.

If the session bean instance stores in its conversational state an object reference to the javax.ejb.SessionContext interface passed to the instance in the setSessionContext(...) method, the container must be able to save and restore the reference across the instance's passivation. The container can replace the original SessionContext object with a different and functionally equivalent SessionContext object during activation.

If the session bean instance stores in its conversational state an object reference to the java:comp/env JNDI context or its subcontext, the container must be able to save and restore the object reference across the instance's passivation. The container can replace the original object with a different and functionally equivalent object during activation.

If the session bean instance stores in its conversational state an object reference to the UserTransaction interface, the container must be able to save and restore the object reference across the instance's passivation. The container can replace the original object with a different and functionally equivalent object during activation.

The container may destroy a session bean instance if the instance does not meet the requirements for serialization after ejbPassivate .

While the container is not required to use the serialization protocol for the Java programming language to store the state of a passivated session instance, it must achieve the equivalent result. The one exception is that containers are not required to reset the value of transient fields during activation. [2] Declaring the session bean's fields as transient is, in general, discouraged.

[2] This is to allow the container to swap out an instance's state through techniques other than the Java programming language serialization protocol. For example, the container's Java virtual machine implementation may use a block of memory to keep the instance's variables , and the container swaps the whole memory block to the disk instead of performing Java programming language serialization on the instance.

EJB.6.4.2 The Effect of Transaction Rollback on Conversational State

A session object's conversational state is not transactional. It is not automatically rolled back to its initial state if the transaction in which the object has participated rolls back.

If a rollback could result in an inconsistency between a session object's conversational state and the state of the underlying database, the bean developer (or the application development tools used by the developer) must use the afterCompletion notification to manually reset its state.



Java 2 Platform, Enterprise Edition. Platform and Component Specifications
Java 2 Platform, Enterprise Edition: Platform and Component Specifications
ISBN: 0201704560
EAN: 2147483647
Year: 2000
Pages: 399

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net