Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Enterprise JavaBeans™ Specification, Version 2.0 - Sun Microsystems.pdf
Скачиваний:
14
Добавлен:
24.05.2014
Размер:
2.71 Mб
Скачать

Sun Microsystems Inc

Conversational state

Enterprise JavaBeans 2.0, Public Draft

Session Bean Component Contract

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 Java object references.

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.

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[2].

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™ 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.

[2]Note that the Java 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.

59

5/31/00

Sun Microsystems Inc.

Session Bean Component Contract

Enterprise JavaBeans 2.0, Public Draft

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 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 [3]. Declaring the session bean’s fields as transient is, in general, discouraged.

[3]This is to allow the Container to swap out an instance’s state through techniques other than the Java 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 Serialization on the instance.

5/31/00

60