2

I've enabled sessions in App Engine which works perfectly well when the app is deployed. My problem is that this does not appear to work in development mode. I need something to persist sessions somewhere (disk, datastore, memory) in development mode so that I don't have to log into my app every time I restart the local server (which is every time I make changes to server or shared code).

I ordinarily do this by defining a HashSessionManager in jetty-web.xml, but apparently (and understandably) App Engine explicitly disables that config file for security reasons.

Does anyone know the standard way of achieving local session persistence in App Engine, assuming one exists?

I have the following related lines in appengine-web.xml:

  <sessions-enabled>true</sessions-enabled>
  <async-session-persistence enabled="true" />

I'm using version 1.6.5 (latest) of the App Engine SDK.

Here are some steps I've taken:

In my server-side class extending RemoteServiceServlet, I added the following line:

public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    // Added this line
    System.out.println(request.getSession().getCreationTime());
    ...
}

I restarted the dev mode server, loaded my app and got "1337796704817" for every request my app made, indicating the same session is in use between requests. Then, I restarted the dev mode server, and the next request produced "1337796798184", indicating a new session had been created.

As noted below, I am using Google Cloud SQL, and I've disabled the datastore options in app engine settings, though this doesn't seem to cause any problems when deployed. Also, I tried the same steps above with those options checked, and got the same results.

tangent
  • 388
  • 1
  • 4
  • 14

2 Answers2

0

Sessions are persisted to the datastore. As long as you're not clearing the datastore each time you restart the dev_appserver, your sessions should remain. Check that you're not clearing the datastore on each restart.

Nick Johnson
  • 100,655
  • 16
  • 128
  • 198
  • 1
    So you're saying that as long as I'm not going out of my way to clear the datastore upon startup, sessions should be persisted even in dev mode? Currently it works when deployed, but I've confirmed that development mode sessions are not being written to the datastore, and I'm not clearing it out. One thing to note, I'm not using the datastore for anything else, I'm using Google Cloud SQL, and I have "Enabled local HRD support" and "Use Datanucleus JDO/JPA to access the datastore" unchecked in App Engine settings. – tangent May 22 '12 at 12:58
0

If you have the cookie_args.domain value set (as described in the Webapp2 Sessions), then it won't work on your development server. That is, if the domain is set to .mydomain.com then it won't work from your development server localhost:8000.

For example, if your main.py contains a config object like this:

config = {
    'webapp2_extras.sessions': {
        'secret_key': '**secret_key**',
        'cookie_args': {'domain': '.mydomain.com', 'httponly': True}
    }
}

The solution is to write some conditional code:

import os
domain = None if os.environ['SERVER_SOFTWARE'].startswith('Development') else '.mydomain.com'

config = {
    'webapp2_extras.sessions': {
        'secret_key': '**secret_key**',
        'cookie_args': {'domain': domain, 'httponly': True}
    }
}
WSGIApplication(Routing, config=config, debug=True)
Brent Washburne
  • 12,904
  • 4
  • 60
  • 82