I would very strongly recommend against the first example, using 
request.add_finished_callback to close the session.  add_finished_callback is 
outside main request stack (tweens and view derivers and all that), where the 
error handling tends to be.  Speaking from experience, I was completely 
ignorant of a whole plethora of errors in my app because session errors weren’t 
being captured.

— Theron



> On Sep 1, 2021, at 9:30 AM, Michael Merickel <[email protected]> wrote:
> 
> Unfortunately there's no "right" answer to the situation, it all depends on 
> how you choose to manage database connections in your app.
> 
> It's hard to answer whether what you're doing now is correct or not without 
> knowing more - you can have pyramid_tm enabled in your app and not actually 
> use it. It'll just be a no-op if things don't hook into request.tm and use it 
> (via zope.sqlalchemy or other using request.tm directly).
> 
> The cookiecutter provides what I'd consider to be the best option (but I'm 
> biased) which sets up pyramid_tm to manage the begin/commit on the 
> transaction. However there are perfectly legitimate ways to handle things 
> (such as in your first example) that do not use pyramid_tm with 
> zope.sqlalchemy.
> 
> - Michael
> 
>> On Sep 1, 2021, at 05:53, Petr Blahoš <[email protected] 
>> <mailto:[email protected]>> wrote:
>> 
>> Hi,
>> 
>> I am upgrading some old code of mine, and I came across the
>> request.db (I think it's called request.session in Pyramid scaffolding)
>> method which looks like:
>> 
>> def db(request):
>>     session = request.registry.sessionmaker()
>> 
>>     def cleanup(request):
>>         session.close()
>>     request.add_finished_callback(cleanup)
>>     return session
>> 
>> and calling config.add_request_method(db, reify=True) somewhere, while the 
>> sessionmaker was called with ZopeTransactionExtension.
>> 
>> The "current" version is something like:
>> def db(request):
>>     session = request.registry.sessionmaker()
>>     zope.sqlalchemy.register(session, transaction_manager=request.tm 
>> <http://request.tm/>)
>>     return session
>> 
>> Now, I wonder. I expect that pyramid_tm takes care of the cleanup / 
>> session.close()
>> for the request. But I have always used the transaction manager pyramid_tm. 
>> Am I 
>> guessing correctly, that adding finished callback(cleanup) was unnecessary?
>> 
>> Thanks...
>> Petr
>> -- 
>> https://blahos.com/ <https://blahos.com/>
>> https://traceability.cz/ <https://traceability.cz/>
>> 
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "pylons-discuss" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected] 
>> <mailto:[email protected]>.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/pylons-discuss/CA%2ByMeXWHgTrt%3DTHkK-fgAcdjxxpqe3nQkuy1RBg4ziM61Axazg%40mail.gmail.com
>>  
>> <https://groups.google.com/d/msgid/pylons-discuss/CA%2ByMeXWHgTrt%3DTHkK-fgAcdjxxpqe3nQkuy1RBg4ziM61Axazg%40mail.gmail.com?utm_medium=email&utm_source=footer>.
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "pylons-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected] 
> <mailto:[email protected]>.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/pylons-discuss/7F3DB6DE-3AFD-4AF3-A769-72358ABAC5BA%40gmail.com
>  
> <https://groups.google.com/d/msgid/pylons-discuss/7F3DB6DE-3AFD-4AF3-A769-72358ABAC5BA%40gmail.com?utm_medium=email&utm_source=footer>.

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pylons-discuss/70479627-73E8-4B44-9AE4-5565344990A9%40luhn.com.

Reply via email to