[Zope-dev] Re: SQLAlchemy (zope.sqlalchemy) integration

Laurence Rowe l at lrowe.co.uk
Thu Jun 5 13:38:26 EDT 2008


Hermann Himmelbauer wrote:
> Am Mittwoch, 4. Juni 2008 22:09 schrieb Laurence Rowe:
>> Hermann Himmelbauer wrote:
>>> In my application, I then use getSASession() to retrieve my session.
>>>
>>> However, what I think is not that beautiful is the "s.bind = engine"
>>> part. Are there any suggestions how to improve this?
>> You have two options
>>
>> If you ever need to mix objects from different `sites` into the same
>> session, you should use an adapter on your root object like:
>>
>> If you don't need to mix objects from different `sites` then you can
>> register a local utility for ISessionConfig.
>>
>> def scope():
>>    return getUtility(ISessionConfig).uid, thread.get_ident()
>>
>> def factory():
>>    engine = Engine(getUtility(ISessionConfig).url)
>>    return create_session(
>>      transactional=True, autoflush=True, bind=engine
>>      extension=ZopeTransactionExtension(),
>>      ))
>>
>> Session = scoped_session(factory, scopefunc=scope)
>>
>> Then you can just import Session and use:
>>     session = Session()
> 
> Ok, great, thanks for help. The only thing I don't understand is what "uid" 
> from the SessionConfig utility is. Below is my full database integration code 
> which works for me, perhaps this is helpful to someone else.

uid is some id that distinguishes your various application instances. On 
zope 2 I would probably use getPhysicalPath(). I don't know what the 
zope3 equivalent is.

Looking at your code, why did you decide to store the engine on a _v_ 
attribute? I don't think you need to save it at all. You can access a 
connection through session.connection()


> Btw., I'd suggest to put such code / session use cases in some Zope package, 
> maybe into zope.sqlalchemy, or e.g. zope.sqlalchemy_utility as it's really 
> difficult for non-insiders to set this up.

We would need to work out what parts are useful to the various higher 
level sqlalchemy / zope packages. Once we can agree on a common core 
then we should at least make simple use cases available through 
zope.sqlalchemy directly.

Laurence



More information about the Zope-Dev mailing list