Skip to content

Instantly share code, notes, and snippets.

@mmerickel
Created September 13, 2019 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmerickel/3652e5c45d751b27cab66df1e22f70cf to your computer and use it in GitHub Desktop.
Save mmerickel/3652e5c45d751b27cab66df1e22f70cf to your computer and use it in GitHub Desktop.
from .tm_context import tm_context
with tm_context(request.tm):
...
from contextlib import contextmanager, suppress
from transaction.interfaces import NoTransaction
class DoomedAbort(Exception):
pass
@contextmanager
def tm_context(tm):
tm.begin()
try:
yield tm
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