开发者

Session data not persisting in GAE (Java)

开发者 https://www.devze.com 2023-04-10 04:18 出处:网络
I am struggling while handling sessions in GAE. I am trying to store a two classes and a string in session. Although on DEV environment it runs fine, on production a class and a string are not being p

I am struggling while handling sessions in GAE. I am trying to store a two classes and a string in session. Although on DEV environment it runs fine, on production a class and a string are not being persisted in session. The class that is not getting saved as a session attribute is as follows:

@PersistenceCapable(detachable="true")
public class Agent implements Serializable{
  @PrimaryKey
  @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
  private Long id;

  @Persistent private String name;  //Name of the Agency
  @Element(dependent = "true") 
  private List<Contact> contacts = new ArrayList<Contact>();

  @Element(dependent = "true") 
  private List<Agency> agencies = new ArrayList<Agency>();

  @Persistent private List<Long> subAgents = new ArrayList<Long>();

  @Persistent private Date createdOn = new Date();  
}

I would like开发者_运维百科 to mention again that it works fine on DEV Environment but on production I get values as null. As you can see I have made the class implement Serializable. But I think it is not the problem because I am setting one more attribute as a simple string and that also is failing (I get the attribute value as null). Session however is created as I can see it at the backend and also there is one more class which is persisted in session.

Anybody have suggestions? Thanks in advance.


Your problem is probably related to either:

  1. GAE often serializes sessions almost immediately, dev environment doesn't. So all objects in your graph must implement Serializable.

  2. BUT EVEN MORE LIKELY is that after you modify a session variable, you must do something like req.getSession().setAttribute(myKey,myObj) - it WILL NOT see changes in your object and automatically write them back to the session... so the session attributes will have the value of whatever they had when they were last set.

Problem #2 above cost me countless time and pain until I tripped over (via a lengthy process of elimination).


Have you enabled sessions in your configuration file? http://code.google.com/intl/en/appengine/docs/java/config/appconfig.html#Enabling_Sessions


Making classes Agency and Contact Serializable solves the problem. That mean each and every object (be it nested or otherwise) which is present inside a session attribute should be serializable.

0

精彩评论

暂无评论...
验证码 换一张
取 消