stories icon indicating copy to clipboard operation
stories copied to clipboard

Document Django Atomic transaction management.

Open proofit404 opened this issue 5 years ago • 0 comments

As mentioned in #432 the Atomic context manager isn't that straightforward to use with stories.

https://github.com/django/django/blob/948a874425e7d999950a8fa3b6598d9e34a4b861/django/db/transaction.py#L168-L194

As you can see the class has the __exit__ method which is used both for commit and rollback methods. That's a limitation we need to deal with.

>>> class Persistence:
...
...     def __init__(self):
...         self.started = False
...         self.commited = False
...         self.atomic = Atomic()
...
...     def start_transaction(self):
...         self.started = True
...         self.atomic.__enter__()
...
...     def end_transaction(self):
...         self.commited = True
...         self.atomic.__exit__(None, None, None)
...
...     def finalize(self):
...         if self.started and not self.commited:
...             self.atomic.__exit__(Exception(), None, None)

proofit404 avatar Jul 29 '20 09:07 proofit404