[Zope-dev] Re: zope.sqlalchemy, integration ideas

Martijn Faassen faassen at startifact.com
Mon May 26 11:58:14 EDT 2008


Laurence Rowe wrote:
> Brian Sutherland wrote:
[snip]
>> I'm not sure what "in this context" means?
> 
> The context of this application instance. Say you have two instances of 
> an application, one pointed at database A, another at database B. It is 
> possible to involve both applications in a single request / transaction. 
> You must be sure that you are working with the correct session.

I should've read the whole thread before I asked the same question as Brian.

I think the use case of involving two applications in the same 
transaction is a exceptional use case, and therefore passing in context 
manually each and every time isn't warranted. I'd prefer it if we simply 
used Zope 3's existing mechanisms for this, where getUtility allows the 
explicit passing of a context attribute *if desired*, instead of 
wrapping it in an adapter and requiring an explicit context just for that.

Passing in context all the time would theoretically allow existing code 
to work unchanged when two applications are involved in the same 
transaction. They'd both get the right session. I'll note that Zope 3 
itself doesn't actually provide for this use case for its own local 
utility system - it encourages people to look up utility *without* 
context, and thus if two sites are in play (especially when non-nested), 
quite unexpected things can happen when one calls into the other.

For that reason, I argued against the thread-local changes that Jim made 
years ago. Jim wisely went ahead and did it anyway, and I've since come 
to appreciate the alteration, as it just makes life a lot easier for the 
programmer not to have to worry about context all the time. I never 
actually saw much unexpected happen in practice. So for the same reason 
I propose we follow Zope's lead and don't place a burden on all 
developers for this rather theoretical use case.

>>
>>> Of course it would be possible to register a ScopedSession globally 
>>> as such a utility, but more usually a local utility should be 
>>> registered.
>>
>> Depends what you're doing. If you are running without a ZODB, you have
>> mostly just global utilities.
>>
>> It would be a pity if zope.sqlalchemy started to depend on the ZODB.
> 
> Wihout ZODB and zope.app.component it seems unlikely that you would be 
> able to register a local utility, the idea of ISessionContext is so that 
> you might be able to register a ScopedSession as a global utility too.

local utilities are not necessarily persistent, so you could still have 
local sites with local site managers without the ZODB.

I'd argue in this case just using SQLAlchemy's ScopedSession mechanism 
works fine for scoping sessions however, and we don't go play with 
utilities for sessions, just for engines.

Regards,

Martijn



More information about the Zope-Dev mailing list