Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

transaction.py 11KB

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