pythondatabasesqlalchemypyramidbeaker

Storing session data in database in Pyramid using beaker


I'm building a web application in Pyramid and it needs user logins. Database backend is a MySQL DB connected via SQLAalchemy.

Pyramid has an introduction on using beaker for sessions, but it only shows how to configure it using files. I couldn't find out how to store session data in the database (I think it should be possible), since then I would have only one place were my varying data is stored.


Solution

  • I found it. Put something like this in your configuration file (development.ini/production.ini)

    session.type=ext:database
    session.secret=someThingReallyReallySecret
    session.cookie_expires=true
    session.key=WhatEver
    session.url=mysql://user:password@host/database
    session.timeout=3000
    session.lock_dir=%(here)s/var/lock
    

    I don't is if it is possible (or sensible) to put locking to the DB, too, but the sessions should live in the DB like this. You'll need to take care to delete old sessions from the DB yourself (but I think that's the case when using files, too).