zopefoundation / transaction

Generic transaction implementation for Python.
Other
70 stars 31 forks source link

Question: Nested transaction.manager #109

Closed coler-j closed 2 years ago

coler-j commented 2 years ago

What I did:

What would the following do:

with transaction.manager() as t1:
    # some logic
    with transaction.manager() as t2:
        # some logic

What I expect to happen:

The inner transaction.manager would create a nested transaction under t1

jamadden commented 2 years ago

If you're looking for something that you can independently rollback, but which (if not rolledback) still commits when the outer transaction commits, perhaps you want a savepoint:

tx = transaction.begin()
do_stuff()
savepoint = tx.savepoint()
try:
   do_optional_stuff()
except Exception:
   # Discard whatever the optional stuff did
   savepoint.rollback()
# Commit whatever do_stuff() did, and if not rolled back, also 
# the optional stuff
tx.commit()
coler-j commented 2 years ago

Yes that is what I am looking for. Was hoping the transaction.manager was contextually aware that it should use a savepoint if it was nested (like Django's transaction.atomic decorator