Why would rollback() not be sufficient

22 views
Skip to first unread message

Lee Doolan

unread,
Dec 2, 2019, 2:02:46 PM12/2/19
to sqlalchemy
I recently encountered this little snippet of code at the beginning of a transaction

            # I am surprised we need to call both of these, but
           
# we do.  Neither alone will prevent stale data from
           
# persisting in the session.
            meta
.Session.expunge_all()
            meta
.Session.rollback()


I have looked through the SQLA documentation (1.3) and I cannot find any reason 
why simply using a rollback() would not suffice here.  Maybe it was necessary when the code
was written for an older version of SQLA.

Does anyone have any thoughts on the matter?

Mike Bayer

unread,
Dec 2, 2019, 3:18:48 PM12/2/19
to noreply-spamdigest via sqlalchemy
Hi there -

expunge_all() and rollback() do different things, and both have a purpose.   As far as what will "suffice" it depends on what exactly it is that you are attempting to accomplish.

I would also note that calling session.close() has the same effect as that of the two statements above. 

A typical reason one might want to use both is so that any Python objects which are attached to this Session are no longer attached, and as such when their unloaded attributes are accessed, no SQL statement or implicit transaction will be started again on the original Session.









--
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.

Reply all
Reply to author
Forward
0 new messages