10.8.4 Persistent sessions and non-serializable J2EE objects
In order for the WebSphere session manager to persist a session to the persistent store, all of the Java objects in an HttpSession must be serializable. They must implement the java.io.Serializable interface. The HttpSession can also contain the following J2EE objects, which are not serializable:
| javax.ejb.EJBObject
|
| javax.ejb.EJBHome
|
| javax.naming.Context
|
| javax.transaction.UserTransaction
|
The WebSphere session manager works around the problem of serializing these objects in the following manner:
-
| EJBObject and EJBHome each have Handle and HomeHandle object attributes that are serializable and can be used to reconstruct the EJBObject and EJBHome.
|
-
| Context is constructed with a hash table based environment, which is serializable. WebSphere will retrieve the environment, then wrap it with an internal, serializable object. On reentry, it can check the object type and reconstruct the Context.
|
-
| UserTransaction has no serializable attributes. WebSphere provides two options:
|
a.
The Web developer can place the object in the HttpSession, but WebSphere will not persist it outside the JVM.
|
b.
WebSphere has a new public wrapper object, com.ibm.websphere.servlet.session.UserTransactionWrapper, which is serializable and requires the InitialContext used to construct the UserTransaction. This will be persisted outside the JVM and be used to reconstruct the UserTransaction.
|
According to J2EE, a Web component can only start a transaction in a service method. A transaction that is started by a servlet or JSP must be completed before the service method returns. That is, transactions cannot span Web requests from a client. If there is an active transaction after returning from the service method, WebSphere will detect it and abort the transaction.
|
In general, Web developers should consider making all other Java objects held by HttpSession serializable, even if immediate plans do not call for the use of persistent session management. If the Web site grows, and persistent session management becomes necessary, the transition between local and persistent management occurs transparently to the application if the sessions hold only serializable objects. If not, a switch to persistent session management requires coding changes to make the session contents serializable.
|