Session Handles

From Resin 3.0

(Difference between revisions)
Jump to: navigation, search
 
 
Line 1: Line 1:
 +
[[Category:Sessions]]
 +
 
Servlet sessions, i.e. [[HttpSession]], are designed to hold small, semi-static information about a user.  Sessions are not designed to hold large amounts of data or to manage rapidly changing data like a shopping cart.
 
Servlet sessions, i.e. [[HttpSession]], are designed to hold small, semi-static information about a user.  Sessions are not designed to hold large amounts of data or to manage rapidly changing data like a shopping cart.
  
Line 11: Line 13:
 
   public class UserHandle {
 
   public class UserHandle {
 
     private int _id;
 
     private int _id;
 
+
 
     UserHandle(int id)
 
     UserHandle(int id)
 
     {
 
     {
Line 23: Line 25:
 
     {
 
     {
 
       try {
 
       try {
Context ic = new InitialContext();
+
        Context ic = new InitialContext();
+
 
EntityManager manager
+
        EntityManager manager
  = (EntityManager) ic.lookup("java:comp/env/EntityManager");
+
          = (EntityManager) ic.lookup("java:comp/env/EntityManager");
 
   
 
   
return manager.find(User.class, _id);
+
        return manager.find(User.class, _id);
 
       } catch (Exception e) {
 
       } catch (Exception e) {
throw new RuntimeException(e);
+
        throw new RuntimeException(e);
 
       }
 
       }
 
     }
 
     }

Latest revision as of 17:22, 13 February 2006


Servlet sessions, i.e. HttpSession, are designed to hold small, semi-static information about a user. Sessions are not designed to hold large amounts of data or to manage rapidly changing data like a shopping cart.

A way of handling this discrepancy is to store a handle to the saved object in the session. The java.io.Serialization API uses writeReplace and readResolve to serialize the handle in place of the database object.

UserHandle

The handle is the stored serialized form of the session data. It contains only enough information to load the stored session from the database. It is also static, i.e. the value of the handle never changes, only the object created by the handle does.

The readResolve method converts the serialized UserHandle to the application's User object. In other words, the application only sees the User object. The UserHandle is an implementation class for serialization.

 public class UserHandle {
   private int _id;

   UserHandle(int id)
   {
     _id = id;
   }

   /**
    * When deserializing return the User.
    */
   private Object readResolve()
   {
     try {
       Context ic = new InitialContext();
 	
       EntityManager manager
         = (EntityManager) ic.lookup("java:comp/env/EntityManager");

        return manager.find(User.class, _id);
     } catch (Exception e) {
       throw new RuntimeException(e);
     }
   }
 }

User

The persisted state uses EJB 3.0/Amber to store the data.

The writeReplace method converts the User object to a UserHandle during serialization.

@Entity(access=FIELD)
@Table(name="amber_session_user")
public class User {
  @Id(generate=GeneratorType.AUTO)
  @Column(name="id")
  private int _id;
 
 @Basic
 @Column(name="name")
 private String _name;

  public User()
  {
  }

  public User(String name)
  {
    _name = name;
  }
 
  /**
    * Returns the ID of the user.
    */
  public int getId()
  {
    return _id;
  }
 
  public void setId(int id)
  {
    _id = id;
  }

 /**
   * Returns the user's name
   */
  public String getName()
  {
    return _name;
  }

  /**
   * When serializing, replace with the UserHandle instead.
   */
  private Object writeReplace()
  {
    return new UserHandle(_id);
  }
}
Personal tools