[Zope-dev] Re: SQLAlchemy integration experiment

Brian Sutherland brian at vanguardistas.net
Tue Jun 17 11:42:11 EDT 2008


On Tue, Jun 17, 2008 at 05:25:56PM +0200, Brian Sutherland wrote:
> > class IDatabase(Interface):
> >     def scopefunc():
> >           """The scopefunc"""
> > 
> >     def session_factory():
> >           """The session factory"""
> > 
> > def scopefunc():
> >      util = component.getUtility(IDatabase)
> >      return util.scopefunc()
> > 
> > This will allow you to implement a global IDatabase utility that's
> > global and uses a thread-local scope only.
> > Similarly, we can delegate session creation to the IDatabase utility completely:
> > 
> > def session_factory():
> >    util = component.getUtility(IDatabase)
> >    return util.session_factory()
> 
> I think you missed the final piece:
> 
> Session = scoped_session(session_factory, scopefunc=scopefunc)
> 
> > I think that's better than what I have now, as you can then completely
> > control session creation through a utility. 
> 
> Yep
> 
> > It doesn't add a new
> > plugin point or utility lookup, but allows you to implement your use
> > case, right? We'll just need to implement a number of utilities that
> > fulfill the various use cases.
> 
> Yes, it looks pretty trivial to plug in a normal sqlalchemy session.

This would probably be close to what I would write for my usecase:

class Database:

    implements(IDatabase)

    def __init__(self, *args, **kw):
        self._args = args
        self._kw = kw

    def scopefunc(self):
        return None # use default per-thread scoping

    def session_factory(self):
        return sessionmaker(*self._args, **self._kw)

-- 
Brian Sutherland


More information about the Zope-Dev mailing list