[Zope-dev] Re: SQLAlchemy integration experiment

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


On Tue, Jun 17, 2008 at 03:52:51PM +0200, Martijn Faassen wrote:
> Hi there,
> 
> Just as some context: I'm not proposing to extend zope.sqlalchemy at
> all.

I was;)

> I'm proposing to write an extension that has the configuration
> pattern I sketched out. I also don't intend to write the last
> SQLAlchemy integration layer; I wouldn't be so presumptious. :) That
> said, if we can come up with something we all feel comfortable with,
> hopefully the configuration package will be used by others as well.

I see.

> On Tue, Jun 17, 2008 at 3:36 PM, Brian Sutherland
> <brian at vanguardistas.net> wrote:
> [snip]
> > Hmm, how about this approach. It builds on and is almost compatible
> > with yours, but allows me a plug in point where I need it. My use case
> > is the simplest in that I have no local utilities and no need of
> > application scoped sessions (plain old thread scoping works just fine).
> >
> > But I would like to have the option of using code others write for
> > zope/sqlalchemy and allow others to use our code.
> >
> > The total code I would write in zope.sqlalchemy would be this:
> >
> >    class ISession(Interface):
> >
> >        def __call__():
> >            """Return a SQLAlchemy Session object"""
> >
> >    def Session():
> >        return getUtility(ISession)()
> >
> > I would use it in view code like:
> >
> >    from zope.sqlalchemy import Session
> >
> >    class View:
> >
> >        def some_view_method(self):
> >            session = Session()
> >            session.do_stuff()
> >
> > I do believe that the application scoping approach you showed us could
> > be implemented by registering your session as the ISession utility.
> 
> I'm not sure it makes sense to add an extra utility lookup each time I
> get a session. Right now the scoped session just gets the thing if
> it's already cached, and there's already a local utility lookup to get
> the scope UID.

Yep.

> What about this?

It's much better than what I suggested:) It also fills my usecase
completely.

I'm +lots on it.

> 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.

> 
> Regards,
> 
> Martijn
> 

-- 
Brian Sutherland


More information about the Zope-Dev mailing list