[Zope-dev] Re: zope.sqlalchemy

Laurence Rowe l at lrowe.co.uk
Wed May 7 12:11:27 EDT 2008


Martijn Faassen wrote:
> Hi there,
> 
> Laurence Rowe wrote:
> [snip]
>> The code would get a session through:
>>
>>  >>> Session = getUtility(IScopedSession, 'my-app')
>>  >>> session = Session()
> 
> The drawback is that this is more typing. You do a utility lookup and an 
> instantiation as opposed to simply importing the scoped session when 
> needed:
> 
> from myapplication import session
> 
> session.query(...)
> 
> One topic we ran into during the megrok.kss discussion is doing multiple 
> instances of the same application, each writing to a different database. 
>   You can't hardcode your database name in your application. I think 
> sessions as local utilities would help us solve that problem, right?
> 
> What would be nice if we could do the 'from myapp import session' 
> pattern and have it use the local utility infrastructure underneath 
> somehow... Possible?


We'll have to stick with scoped sesssions because of threading, but the 
engine as local utility pattern should still work.

#myapplication/__init__.py
Session = scoped_session(sessionmaker(bind=LookupEngine('my-engine')...))
engine = EngineUtility(url='sqlite:///')
provideUtility(engine, IConnectable, 'my-engine') # but normally a local 
utility registration

#myapplication/foo.py
from myapplication import Session
session = Session()

One (perhaps the only) advantage I can see with looking up the scoped 
session as a utility is that it gives the integrator control over 
whether to use one or two phase commit, as this is set in the session 
configuration. Normally one would prefer one-phase commit as it is 
faster, but if an integrator arranged for two applications to be 
modified in a single transaction she would want to configure two-phase 
commit.

Laurence



More information about the Zope-Dev mailing list