On Mon, Nov 22, 2010 at 10:45:34AM -0800, Josh Kelley wrote:
> I finally found the root cause of the "MySQL session has gone away"
> errors that I posted about in
> http://groups.google.com/group/pylons-discuss/browse_thread/thread/c0f1fe15c841303.
> I'm using repoze.what.plugins.quickstart, following the example at
> http://wiki.pylonshq.com/display/pylonscookbook/Authorization+with+repoze.what:
> 
>     from admin.model.meta import Session
...
> 
> Apparently meta.Session is imported here before it's assigned its
> final value by admin.model.init_model, so even though add_auth is
> executed after init_model, it gets the pre-init_model version of
> Session, so it ends up using its own session, separate from the rest
> of the app, that's never removed and never recycled.

Ah, ouch.

Well, the templates that come with Pylons 1.0 avoid reassigning any
global names in model.meta.  Instead, model/__init__.py looks like this:

    from (project).model.meta import Session, Base

    def init_model(engine):
        """Call me before using any of the tables or classes in the model"""
        Session.configure(bind=engine)

(Incidentally, it would be useful to have a sample project created using
the latest templates on the web somewhere.  Say, github; updated
automatically whenever you change the template, so that, for example,
people could see the differences between a "standard" Pylons 0.97
project and a Pylons 1.0 one.)

> I guess I'm confused now as to why meta.Session is set up the way it
> is.  This approach seems like it can introduce subtle bugs; what's the
> best way to avoid bugs like this in the future?

Do the same thing as above: avoid assignments to globals.

> Specific questions:
> 
> Should I update the wiki.pylonshq.com article to use meta.Session
> instead of importing Session directly?

That might be safer, for people who have older projects with the unsafe
init_model.  However the new convention seems to be from foo.model.meta
import Session.

Marius Gedminas
-- 
Cheap, Fast, Good -- pick two.

Attachment: signature.asc
Description: Digital signature

Reply via email to