transaction.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. from contextlib import ContextDecorator, contextmanager
  2. from django.db import (
  3. DEFAULT_DB_ALIAS,
  4. DatabaseError,
  5. Error,
  6. ProgrammingError,
  7. connections,
  8. )
  9. class TransactionManagementError(ProgrammingError):
  10. """Transaction management is used improperly."""
  11. pass
  12. def get_connection(using=None):
  13. """
  14. Get a database connection by name, or the default database connection
  15. if no name is provided. This is a private API.
  16. """
  17. if using is None:
  18. using = DEFAULT_DB_ALIAS
  19. return connections[using]
  20. def get_autocommit(using=None):
  21. """Get the autocommit status of the connection."""
  22. return get_connection(using).get_autocommit()
  23. def set_autocommit(autocommit, using=None):
  24. """Set the autocommit status of the connection."""
  25. return get_connection(using).set_autocommit(autocommit)
  26. def commit(using=None):
  27. """Commit a transaction."""
  28. get_connection(using).commit()
  29. def rollback(using=None):
  30. """Roll back a transaction."""
  31. get_connection(using).rollback()
  32. def savepoint(using=None):
  33. """
  34. Create a savepoint (if supported and required by the backend) inside the
  35. current transaction. Return an identifier for the savepoint that will be
  36. used for the subsequent rollback or commit.
  37. """
  38. return get_connection(using).savepoint()
  39. def savepoint_rollback(sid, using=None):
  40. """
  41. Roll back the most recent savepoint (if one exists). Do nothing if
  42. savepoints are not supported.
  43. """
  44. get_connection(using).savepoint_rollback(sid)
  45. def savepoint_commit(sid, using=None):
  46. """
  47. Commit the most recent savepoint (if one exists). Do nothing if
  48. savepoints are not supported.
  49. """
  50. get_connection(using).savepoint_commit(sid)
  51. def clean_savepoints(using=None):
  52. """
  53. Reset the counter used to generate unique savepoint ids in this thread.
  54. """
  55. get_connection(using).clean_savepoints()
  56. def get_rollback(using=None):
  57. """Get the "needs rollback" flag -- for *advanced use* only."""
  58. return get_connection(using).get_rollback()
  59. def set_rollback(rollback, using=None):
  60. """
  61. Set or unset the "needs rollback" flag -- for *advanced use* only.
  62. When `rollback` is `True`, trigger a rollback when exiting the innermost
  63. enclosing atomic block that has `savepoint=True` (that's the default). Use
  64. this to force a rollback without raising an exception.
  65. When `rollback` is `False`, prevent such a rollback. Use this only after
  66. rolling back to a known-good state! Otherwise, you break the atomic block
  67. and data corruption may occur.
  68. """
  69. return get_connection(using).set_rollback(rollback)
  70. @contextmanager
  71. def mark_for_rollback_on_error(using=None):
  72. """
  73. Internal low-level utility to mark a transaction as "needs rollback" when
  74. an exception is raised while not enforcing the enclosed block to be in a
  75. transaction. This is needed by Model.save() and friends to avoid starting a
  76. transaction when in autocommit mode and a single query is executed.
  77. It's equivalent to:
  78. connection = get_connection(using)
  79. if connection.get_autocommit():
  80. yield
  81. else:
  82. with transaction.atomic(using=using, savepoint=False):
  83. yield
  84. but it uses low-level utilities to avoid performance overhead.
  85. """
  86. try:
  87. yield
  88. except Exception as exc:
  89. connection = get_connection(using)
  90. if connection.in_atomic_block:
  91. connection.needs_rollback = True
  92. connection.rollback_exc = exc
  93. raise
  94. def on_commit(func, using=None, robust=False):
  95. """
  96. Register `func` to be called when the current transaction is committed.
  97. If the current transaction is rolled back, `func` will not be called.
  98. """
  99. get_connection(using).on_commit(func, robust)
  100. #################################
  101. # Decorators / context managers #
  102. #################################
  103. class Atomic(ContextDecorator):
  104. """
  105. Guarantee the atomic execution of a given block.
  106. An instance can be used either as a decorator or as a context manager.
  107. When it's used as a decorator, __call__ wraps the execution of the
  108. decorated function in the instance itself, used as a context manager.
  109. When it's used as a context manager, __enter__ creates a transaction or a
  110. savepoint, depending on whether a transaction is already in progress, and
  111. __exit__ commits the transaction or releases the savepoint on normal exit,
  112. and rolls back the transaction or to the savepoint on exceptions.
  113. It's possible to disable the creation of savepoints if the goal is to
  114. ensure that some code runs within a transaction without creating overhead.
  115. A stack of savepoints identifiers is maintained as an attribute of the
  116. connection. None denotes the absence of a savepoint.
  117. This allows reentrancy even if the same AtomicWrapper is reused. For
  118. example, it's possible to define `oa = atomic('other')` and use `@oa` or
  119. `with oa:` multiple times.
  120. Since database connections are thread-local, this is thread-safe.
  121. An atomic block can be tagged as durable. In this case, raise a
  122. RuntimeError if it's nested within another atomic block. This guarantees
  123. that database changes in a durable block are committed to the database when
  124. the block exists without error.
  125. This is a private API.
  126. """
  127. def __init__(self, using, savepoint, durable):
  128. self.using = using
  129. self.savepoint = savepoint
  130. self.durable = durable
  131. self._from_testcase = False
  132. def __enter__(self):
  133. connection = get_connection(self.using)
  134. if (
  135. self.durable
  136. and connection.atomic_blocks
  137. and not connection.atomic_blocks[-1]._from_testcase
  138. ):
  139. raise RuntimeError(
  140. "A durable atomic block cannot be nested within another "
  141. "atomic block."
  142. )
  143. if not connection.in_atomic_block:
  144. # Reset state when entering an outermost atomic block.
  145. connection.commit_on_exit = True
  146. connection.needs_rollback = False
  147. if not connection.get_autocommit():
  148. # Pretend we're already in an atomic block to bypass the code
  149. # that disables autocommit to enter a transaction, and make a
  150. # note to deal with this case in __exit__.
  151. connection.in_atomic_block = True
  152. connection.commit_on_exit = False
  153. if connection.in_atomic_block:
  154. # We're already in a transaction; create a savepoint, unless we
  155. # were told not to or we're already waiting for a rollback. The
  156. # second condition avoids creating useless savepoints and prevents
  157. # overwriting needs_rollback until the rollback is performed.
  158. if self.savepoint and not connection.needs_rollback:
  159. sid = connection.savepoint()
  160. connection.savepoint_ids.append(sid)
  161. else:
  162. connection.savepoint_ids.append(None)
  163. else:
  164. connection.set_autocommit(
  165. False, force_begin_transaction_with_broken_autocommit=True
  166. )
  167. connection.in_atomic_block = True
  168. if connection.in_atomic_block:
  169. connection.atomic_blocks.append(self)
  170. def __exit__(self, exc_type, exc_value, traceback):
  171. connection = get_connection(self.using)
  172. if connection.in_atomic_block:
  173. connection.atomic_blocks.pop()
  174. if connection.savepoint_ids:
  175. sid = connection.savepoint_ids.pop()
  176. else:
  177. # Prematurely unset this flag to allow using commit or rollback.
  178. connection.in_atomic_block = False
  179. try:
  180. if connection.closed_in_transaction:
  181. # The database will perform a rollback by itself.
  182. # Wait until we exit the outermost block.
  183. pass
  184. elif exc_type is None and not connection.needs_rollback:
  185. if connection.in_atomic_block:
  186. # Release savepoint if there is one
  187. if sid is not None:
  188. try:
  189. connection.savepoint_commit(sid)
  190. except DatabaseError:
  191. try:
  192. connection.savepoint_rollback(sid)
  193. # The savepoint won't be reused. Release it to
  194. # minimize overhead for the database server.
  195. connection.savepoint_commit(sid)
  196. except Error:
  197. # If rolling back to a savepoint fails, mark for
  198. # rollback at a higher level and avoid shadowing
  199. # the original exception.
  200. connection.needs_rollback = True
  201. raise
  202. else:
  203. # Commit transaction
  204. try:
  205. connection.commit()
  206. except DatabaseError:
  207. try:
  208. connection.rollback()
  209. except Error:
  210. # An error during rollback means that something
  211. # went wrong with the connection. Drop it.
  212. connection.close()
  213. raise
  214. else:
  215. # This flag will be set to True again if there isn't a savepoint
  216. # allowing to perform the rollback at this level.
  217. connection.needs_rollback = False
  218. if connection.in_atomic_block:
  219. # Roll back to savepoint if there is one, mark for rollback
  220. # otherwise.
  221. if sid is None:
  222. connection.needs_rollback = True
  223. else:
  224. try:
  225. connection.savepoint_rollback(sid)
  226. # The savepoint won't be reused. Release it to
  227. # minimize overhead for the database server.
  228. connection.savepoint_commit(sid)
  229. except Error:
  230. # If rolling back to a savepoint fails, mark for
  231. # rollback at a higher level and avoid shadowing
  232. # the original exception.
  233. connection.needs_rollback = True
  234. else:
  235. # Roll back transaction
  236. try:
  237. connection.rollback()
  238. except Error:
  239. # An error during rollback means that something
  240. # went wrong with the connection. Drop it.
  241. connection.close()
  242. finally:
  243. # Outermost block exit when autocommit was enabled.
  244. if not connection.in_atomic_block:
  245. if connection.closed_in_transaction:
  246. connection.connection = None
  247. else:
  248. connection.set_autocommit(True)
  249. # Outermost block exit when autocommit was disabled.
  250. elif not connection.savepoint_ids and not connection.commit_on_exit:
  251. if connection.closed_in_transaction:
  252. connection.connection = None
  253. else:
  254. connection.in_atomic_block = False
  255. def atomic(using=None, savepoint=True, durable=False):
  256. # Bare decorator: @atomic -- although the first argument is called
  257. # `using`, it's actually the function being decorated.
  258. if callable(using):
  259. return Atomic(DEFAULT_DB_ALIAS, savepoint, durable)(using)
  260. # Decorator: @atomic(...) or context manager: with atomic(...): ...
  261. else:
  262. return Atomic(using, savepoint, durable)
  263. def _non_atomic_requests(view, using):
  264. try:
  265. view._non_atomic_requests.add(using)
  266. except AttributeError:
  267. view._non_atomic_requests = {using}
  268. return view
  269. def non_atomic_requests(using=None):
  270. if callable(using):
  271. return _non_atomic_requests(using, DEFAULT_DB_ALIAS)
  272. else:
  273. if using is None:
  274. using = DEFAULT_DB_ALIAS
  275. return lambda view: _non_atomic_requests(view, using)