when does session.transaction come into being?

7 views
Skip to first unread message

Chris Withers

unread,
Jun 4, 2019, 3:04:58 AM6/4/19
to sqlalchemy
Hi All,

What creates session.transaction? I can't spot get __getattr__ magic,
but the only place in the code I see it being created is in .begin(...),
which has a docstring saying that it should no longer be used, so I feel
like I must be missing something?

cheers,

Chris

Mike Bayer

unread,
Jun 4, 2019, 9:47:19 AM6/4/19
to sqlal...@googlegroups.com
self.begin() is called inside the __init__ method of the Session when autocommit is at the default of False.

the reason you can't call it yourself with autocommit=False is that the Session keeps calling it automatically within each rollback() and commit().

which might seem weird but the SessionTransaction doesn't use any database resources for begin(), it's just a logical boundary.




cheers,

Chris

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper


To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Chris Withers

unread,
Jun 4, 2019, 4:34:23 PM6/4/19
to sqlal...@googlegroups.com, Mike Bayer
On 04/06/2019 14:47, Mike Bayer wrote:
>
>
> On Tue, Jun 4, 2019, at 3:05 AM, Chris Withers wrote:
>> Hi All,
>>
>> What creates session.transaction? I can't spot get __getattr__ magic,
>> but the only place in the code I see it being created is in .begin(...),
>> which has a docstring saying that it should no longer be used, so I feel
>> like I must be missing something?
>
>
> self.begin() is called inside the __init__ method of the Session when
> autocommit is at the default of False.

Okay, I must have missed that.

Just double checking: I thought you said that Session() would not block?
If it's calling self.begin(), how come that isn't a blocking operation?

cheers,

Chris

Mike Bayer

unread,
Jun 4, 2019, 6:09:51 PM6/4/19
to Chris Withers, sqlal...@googlegroups.com
begin() is not blocking because it doesn't do anything with the engine or any connections, it just creates a new SessionTransaction object.

Looking at the source however it can block if you are using the session in autocommit mode, then add some pending objects, then say begin(), because it will flush on begin.   So it depends a little bit how you are using the Session.






cheers,

Chris


Reply all
Reply to author
Forward
0 new messages