Last active
July 13, 2018 15:51
-
-
Save mmerickel/7e1911df61cb2bf4007d35e8dbaae8a4 to your computer and use it in GitHub Desktop.
a context for helping with explicit transaction managers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from contextlib import contextmanager, suppress | |
from transaction.interfaces import NoTransaction | |
class DoomedAbort(Exception): | |
pass | |
@contextmanager | |
def tm_context(tm): | |
tm.begin() | |
try: | |
yield tm.get() | |
with suppress(NoTransaction): | |
if tm.isDoomed(): | |
raise DoomedAbort | |
tm.commit() | |
except DoomedAbort: | |
with suppress(NoTransaction): | |
tm.abort() | |
except BaseException as ex: | |
with suppress(NoTransaction): | |
tm.abort() | |
raise ex |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment