How do I make sessions persist across browser/server restarts?
I'm using Google AppEngine.
I'm getting a new session id everytime I restart my browser and/or server.
String jSessionId=this.getThreadLocalRequest().getSession().getId();
End Goal
The big win I'm shooting for is long lived anonymous accounts.
For example, a user can do action A, have an anonymous account created on their behalf, then come back the next day and do action B with the same anonymous account they did action A with(assuming they didn't clear their cookies in between).
And at some point, once they've been drawn in, they can decide to validate/register their account and keep credit for the anonymous stuff they've already done.
I ended up using java.util.UUID.randomUUID().toString() to generate my own unique session ID's.
This is how i managed to do it:
This way the session on GAE is stored for up to 2 weeks of inactivity. And the JSESSIONID on the client is persisted when browser is closed, for up to two weeks of inactivity as well.
This code needs to be called on every request (so abstract it nicely for re-use). It would have been nice if GAE allowed you to set these defaults, but oh well.
(Note that if you are debugging, when a browser sends cookies back to server in request, the max age is set to -1, but the browser itself stores the real expiry)
Unless I'm mistaken here, and I well could be (if Google changes the internals of GAE), GAE uses both memcache and DataStore for session management. Hence, session data will be present in the DataStore during the course of the session.
If you intend to have persistent sessions, you have two possible course of action:
PS: Server restarts on GAE should ideally be considered far and few. Of course, don't count on this, since GAE could have an outage, in which case the application itself would be unavailable.
Session persistent
Tomcat uses JSESSIONID cookie to keep track sessions across requests. However, it does this always,
Which means it's a so-called session cookie and it doesn't survive browser restart.
The only way to get around this is to overwrite JSESSIONID in the response with an expiration time (keep everything else the same).
To persist sessions across Tomcat restart, you should use one of Persistent Managers. See Tomcat documentation for details,
http://tomcat.apache.org/tomcat-5.5-doc/config/manager.html
Please notice that the session managers don't work well if you have multiple instances of the server behind a load balancer.