class Atomic(ContextDecorator):
Guarantee the atomic execution of a given block.
An instance can be used either as a decorator or as a context manager.
When it's used as a decorator, __call__ wraps the execution of the decorated function in the instance itself, used as a context manager.
When it's used as a context manager, __enter__ creates a transaction or a savepoint, depending on whether a transaction is already in progress, and __exit__ commits the transaction or releases the savepoint on normal exit, and rolls back the transaction or to the savepoint on exceptions.
It's possible to disable the creation of savepoints if the goal is to ensure that some code runs within a transaction without creating overhead.
A stack of savepoints identifiers is maintained as an attribute of the connection. None denotes the absence of a savepoint.
This allows reentrancy even if the same AtomicWrapper is reused. For
example, it's possible to define oa = atomic('other')
and use @oa
or
with oa:
multiple times.
Since database connections are thread-local, this is thread-safe.
An atomic block can be tagged as durable. In this case, raise a RuntimeError if it's nested within another atomic block. This guarantees that database changes in a durable block are committed to the database when the block exists without error.
This is a private API.