transaction.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. """
  2. This module implements a transaction manager that can be used to define
  3. transaction handling in a request or view function. It is used by transaction
  4. control middleware and decorators.
  5. The transaction manager can be in managed or in auto state. Auto state means the
  6. system is using a commit-on-save strategy (actually it's more like
  7. commit-on-change). As soon as the .save() or .delete() (or related) methods are
  8. called, a commit is made.
  9. Managed transactions don't do those commits, but will need some kind of manual
  10. or implicit commits or rollbacks.
  11. """
  12. import warnings
  13. from functools import wraps
  14. from django.db import (
  15. connections, DEFAULT_DB_ALIAS,
  16. DatabaseError, Error, ProgrammingError)
  17. from django.utils.decorators import available_attrs
  18. from django.utils.deprecation import RemovedInDjango18Warning
  19. class TransactionManagementError(ProgrammingError):
  20. """
  21. This exception is thrown when transaction management is used improperly.
  22. """
  23. pass
  24. ################
  25. # Private APIs #
  26. ################
  27. def get_connection(using=None):
  28. """
  29. Get a database connection by name, or the default database connection
  30. if no name is provided.
  31. """
  32. if using is None:
  33. using = DEFAULT_DB_ALIAS
  34. return connections[using]
  35. ###########################
  36. # Deprecated private APIs #
  37. ###########################
  38. def abort(using=None):
  39. """
  40. Roll back any ongoing transactions and clean the transaction management
  41. state of the connection.
  42. This method is to be used only in cases where using balanced
  43. leave_transaction_management() calls isn't possible. For example after a
  44. request has finished, the transaction state isn't known, yet the connection
  45. must be cleaned up for the next request.
  46. """
  47. get_connection(using).abort()
  48. def enter_transaction_management(managed=True, using=None, forced=False):
  49. """
  50. Enters transaction management for a running thread. It must be balanced with
  51. the appropriate leave_transaction_management call, since the actual state is
  52. managed as a stack.
  53. The state and dirty flag are carried over from the surrounding block or
  54. from the settings, if there is no surrounding block (dirty is always false
  55. when no current block is running).
  56. """
  57. get_connection(using).enter_transaction_management(managed, forced)
  58. def leave_transaction_management(using=None):
  59. """
  60. Leaves transaction management for a running thread. A dirty flag is carried
  61. over to the surrounding block, as a commit will commit all changes, even
  62. those from outside. (Commits are on connection level.)
  63. """
  64. get_connection(using).leave_transaction_management()
  65. def is_dirty(using=None):
  66. """
  67. Returns True if the current transaction requires a commit for changes to
  68. happen.
  69. """
  70. return get_connection(using).is_dirty()
  71. def set_dirty(using=None):
  72. """
  73. Sets a dirty flag for the current thread and code streak. This can be used
  74. to decide in a managed block of code to decide whether there are open
  75. changes waiting for commit.
  76. """
  77. get_connection(using).set_dirty()
  78. def set_clean(using=None):
  79. """
  80. Resets a dirty flag for the current thread and code streak. This can be used
  81. to decide in a managed block of code to decide whether a commit or rollback
  82. should happen.
  83. """
  84. get_connection(using).set_clean()
  85. def is_managed(using=None):
  86. warnings.warn("'is_managed' is deprecated.",
  87. RemovedInDjango18Warning, stacklevel=2)
  88. def managed(flag=True, using=None):
  89. warnings.warn("'managed' no longer serves a purpose.",
  90. RemovedInDjango18Warning, stacklevel=2)
  91. def commit_unless_managed(using=None):
  92. warnings.warn("'commit_unless_managed' is now a no-op.",
  93. RemovedInDjango18Warning, stacklevel=2)
  94. def rollback_unless_managed(using=None):
  95. warnings.warn("'rollback_unless_managed' is now a no-op.",
  96. RemovedInDjango18Warning, stacklevel=2)
  97. ###############
  98. # Public APIs #
  99. ###############
  100. def get_autocommit(using=None):
  101. """
  102. Get the autocommit status of the connection.
  103. """
  104. return get_connection(using).get_autocommit()
  105. def set_autocommit(autocommit, using=None):
  106. """
  107. Set the autocommit status of the connection.
  108. """
  109. return get_connection(using).set_autocommit(autocommit)
  110. def commit(using=None):
  111. """
  112. Commits a transaction and resets the dirty flag.
  113. """
  114. get_connection(using).commit()
  115. def rollback(using=None):
  116. """
  117. Rolls back a transaction and resets the dirty flag.
  118. """
  119. get_connection(using).rollback()
  120. def savepoint(using=None):
  121. """
  122. Creates a savepoint (if supported and required by the backend) inside the
  123. current transaction. Returns an identifier for the savepoint that will be
  124. used for the subsequent rollback or commit.
  125. """
  126. return get_connection(using).savepoint()
  127. def savepoint_rollback(sid, using=None):
  128. """
  129. Rolls back the most recent savepoint (if one exists). Does nothing if
  130. savepoints are not supported.
  131. """
  132. get_connection(using).savepoint_rollback(sid)
  133. def savepoint_commit(sid, using=None):
  134. """
  135. Commits the most recent savepoint (if one exists). Does nothing if
  136. savepoints are not supported.
  137. """
  138. get_connection(using).savepoint_commit(sid)
  139. def clean_savepoints(using=None):
  140. """
  141. Resets the counter used to generate unique savepoint ids in this thread.
  142. """
  143. get_connection(using).clean_savepoints()
  144. def get_rollback(using=None):
  145. """
  146. Gets the "needs rollback" flag -- for *advanced use* only.
  147. """
  148. return get_connection(using).get_rollback()
  149. def set_rollback(rollback, using=None):
  150. """
  151. Sets or unsets the "needs rollback" flag -- for *advanced use* only.
  152. When `rollback` is `True`, it triggers a rollback when exiting the
  153. innermost enclosing atomic block that has `savepoint=True` (that's the
  154. default). Use this to force a rollback without raising an exception.
  155. When `rollback` is `False`, it prevents such a rollback. Use this only
  156. after rolling back to a known-good state! Otherwise, you break the atomic
  157. block and data corruption may occur.
  158. """
  159. return get_connection(using).set_rollback(rollback)
  160. #################################
  161. # Decorators / context managers #
  162. #################################
  163. class Atomic(object):
  164. """
  165. This class guarantees the atomic execution of a given block.
  166. An instance can be used either as a decorator or as a context manager.
  167. When it's used as a decorator, __call__ wraps the execution of the
  168. decorated function in the instance itself, used as a context manager.
  169. When it's used as a context manager, __enter__ creates a transaction or a
  170. savepoint, depending on whether a transaction is already in progress, and
  171. __exit__ commits the transaction or releases the savepoint on normal exit,
  172. and rolls back the transaction or to the savepoint on exceptions.
  173. It's possible to disable the creation of savepoints if the goal is to
  174. ensure that some code runs within a transaction without creating overhead.
  175. A stack of savepoints identifiers is maintained as an attribute of the
  176. connection. None denotes the absence of a savepoint.
  177. This allows reentrancy even if the same AtomicWrapper is reused. For
  178. example, it's possible to define `oa = @atomic('other')` and use `@oa` or
  179. `with oa:` multiple times.
  180. Since database connections are thread-local, this is thread-safe.
  181. """
  182. def __init__(self, using, savepoint):
  183. self.using = using
  184. self.savepoint = savepoint
  185. def __enter__(self):
  186. connection = get_connection(self.using)
  187. if not connection.in_atomic_block:
  188. # Reset state when entering an outermost atomic block.
  189. connection.commit_on_exit = True
  190. connection.needs_rollback = False
  191. if not connection.get_autocommit():
  192. # Some database adapters (namely sqlite3) don't handle
  193. # transactions and savepoints properly when autocommit is off.
  194. # Turning autocommit back on isn't an option; it would trigger
  195. # a premature commit. Give up if that happens.
  196. if connection.features.autocommits_when_autocommit_is_off:
  197. raise TransactionManagementError(
  198. "Your database backend doesn't behave properly when "
  199. "autocommit is off. Turn it on before using 'atomic'.")
  200. # When entering an atomic block with autocommit turned off,
  201. # Django should only use savepoints and shouldn't commit.
  202. # This requires at least a savepoint for the outermost block.
  203. if not self.savepoint:
  204. raise TransactionManagementError(
  205. "The outermost 'atomic' block cannot use "
  206. "savepoint = False when autocommit is off.")
  207. # Pretend we're already in an atomic block to bypass the code
  208. # that disables autocommit to enter a transaction, and make a
  209. # note to deal with this case in __exit__.
  210. connection.in_atomic_block = True
  211. connection.commit_on_exit = False
  212. if connection.in_atomic_block:
  213. # We're already in a transaction; create a savepoint, unless we
  214. # were told not to or we're already waiting for a rollback. The
  215. # second condition avoids creating useless savepoints and prevents
  216. # overwriting needs_rollback until the rollback is performed.
  217. if self.savepoint and not connection.needs_rollback:
  218. sid = connection.savepoint()
  219. connection.savepoint_ids.append(sid)
  220. else:
  221. connection.savepoint_ids.append(None)
  222. else:
  223. # We aren't in a transaction yet; create one.
  224. # The usual way to start a transaction is to turn autocommit off.
  225. # However, some database adapters (namely sqlite3) don't handle
  226. # transactions and savepoints properly when autocommit is off.
  227. # In such cases, start an explicit transaction instead, which has
  228. # the side-effect of disabling autocommit.
  229. if connection.features.autocommits_when_autocommit_is_off:
  230. connection._start_transaction_under_autocommit()
  231. connection.autocommit = False
  232. else:
  233. connection.set_autocommit(False)
  234. connection.in_atomic_block = True
  235. def __exit__(self, exc_type, exc_value, traceback):
  236. connection = get_connection(self.using)
  237. if connection.savepoint_ids:
  238. sid = connection.savepoint_ids.pop()
  239. else:
  240. # Prematurely unset this flag to allow using commit or rollback.
  241. connection.in_atomic_block = False
  242. try:
  243. if connection.closed_in_transaction:
  244. # The database will perform a rollback by itself.
  245. # Wait until we exit the outermost block.
  246. pass
  247. elif exc_type is None and not connection.needs_rollback:
  248. if connection.in_atomic_block:
  249. # Release savepoint if there is one
  250. if sid is not None:
  251. try:
  252. connection.savepoint_commit(sid)
  253. except DatabaseError:
  254. try:
  255. connection.savepoint_rollback(sid)
  256. except Error:
  257. # If rolling back to a savepoint fails, mark for
  258. # rollback at a higher level and avoid shadowing
  259. # the original exception.
  260. connection.needs_rollback = True
  261. raise
  262. else:
  263. # Commit transaction
  264. try:
  265. connection.commit()
  266. except DatabaseError:
  267. try:
  268. connection.rollback()
  269. except Error:
  270. # An error during rollback means that something
  271. # went wrong with the connection. Drop it.
  272. connection.close()
  273. raise
  274. else:
  275. # This flag will be set to True again if there isn't a savepoint
  276. # allowing to perform the rollback at this level.
  277. connection.needs_rollback = False
  278. if connection.in_atomic_block:
  279. # Roll back to savepoint if there is one, mark for rollback
  280. # otherwise.
  281. if sid is None:
  282. connection.needs_rollback = True
  283. else:
  284. try:
  285. connection.savepoint_rollback(sid)
  286. except Error:
  287. # If rolling back to a savepoint fails, mark for
  288. # rollback at a higher level and avoid shadowing
  289. # the original exception.
  290. connection.needs_rollback = True
  291. else:
  292. # Roll back transaction
  293. try:
  294. connection.rollback()
  295. except Error:
  296. # An error during rollback means that something
  297. # went wrong with the connection. Drop it.
  298. connection.close()
  299. finally:
  300. # Outermost block exit when autocommit was enabled.
  301. if not connection.in_atomic_block:
  302. if connection.closed_in_transaction:
  303. connection.connection = None
  304. elif connection.features.autocommits_when_autocommit_is_off:
  305. connection.autocommit = True
  306. else:
  307. connection.set_autocommit(True)
  308. # Outermost block exit when autocommit was disabled.
  309. elif not connection.savepoint_ids and not connection.commit_on_exit:
  310. if connection.closed_in_transaction:
  311. connection.connection = None
  312. else:
  313. connection.in_atomic_block = False
  314. def __call__(self, func):
  315. @wraps(func, assigned=available_attrs(func))
  316. def inner(*args, **kwargs):
  317. with self:
  318. return func(*args, **kwargs)
  319. return inner
  320. def atomic(using=None, savepoint=True):
  321. # Bare decorator: @atomic -- although the first argument is called
  322. # `using`, it's actually the function being decorated.
  323. if callable(using):
  324. return Atomic(DEFAULT_DB_ALIAS, savepoint)(using)
  325. # Decorator: @atomic(...) or context manager: with atomic(...): ...
  326. else:
  327. return Atomic(using, savepoint)
  328. def _non_atomic_requests(view, using):
  329. try:
  330. view._non_atomic_requests.add(using)
  331. except AttributeError:
  332. view._non_atomic_requests = set([using])
  333. return view
  334. def non_atomic_requests(using=None):
  335. if callable(using):
  336. return _non_atomic_requests(using, DEFAULT_DB_ALIAS)
  337. else:
  338. if using is None:
  339. using = DEFAULT_DB_ALIAS
  340. return lambda view: _non_atomic_requests(view, using)
  341. ############################################
  342. # Deprecated decorators / context managers #
  343. ############################################
  344. class Transaction(object):
  345. """
  346. Acts as either a decorator, or a context manager. If it's a decorator it
  347. takes a function and returns a wrapped function. If it's a contextmanager
  348. it's used with the ``with`` statement. In either event entering/exiting
  349. are called before and after, respectively, the function/block is executed.
  350. autocommit, commit_on_success, and commit_manually contain the
  351. implementations of entering and exiting.
  352. """
  353. def __init__(self, entering, exiting, using):
  354. self.entering = entering
  355. self.exiting = exiting
  356. self.using = using
  357. def __enter__(self):
  358. self.entering(self.using)
  359. def __exit__(self, exc_type, exc_value, traceback):
  360. self.exiting(exc_type, self.using)
  361. def __call__(self, func):
  362. @wraps(func)
  363. def inner(*args, **kwargs):
  364. with self:
  365. return func(*args, **kwargs)
  366. return inner
  367. def _transaction_func(entering, exiting, using):
  368. """
  369. Takes 3 things, an entering function (what to do to start this block of
  370. transaction management), an exiting function (what to do to end it, on both
  371. success and failure, and using which can be: None, indicating using is
  372. DEFAULT_DB_ALIAS, a callable, indicating that using is DEFAULT_DB_ALIAS and
  373. to return the function already wrapped.
  374. Returns either a Transaction objects, which is both a decorator and a
  375. context manager, or a wrapped function, if using is a callable.
  376. """
  377. # Note that although the first argument is *called* `using`, it
  378. # may actually be a function; @autocommit and @autocommit('foo')
  379. # are both allowed forms.
  380. if using is None:
  381. using = DEFAULT_DB_ALIAS
  382. if callable(using):
  383. return Transaction(entering, exiting, DEFAULT_DB_ALIAS)(using)
  384. return Transaction(entering, exiting, using)
  385. def autocommit(using=None):
  386. """
  387. Decorator that activates commit on save. This is Django's default behavior;
  388. this decorator is useful if you globally activated transaction management in
  389. your settings file and want the default behavior in some view functions.
  390. """
  391. warnings.warn("autocommit is deprecated in favor of set_autocommit.",
  392. RemovedInDjango18Warning, stacklevel=2)
  393. def entering(using):
  394. enter_transaction_management(managed=False, using=using)
  395. def exiting(exc_type, using):
  396. leave_transaction_management(using=using)
  397. return _transaction_func(entering, exiting, using)
  398. def commit_on_success(using=None):
  399. """
  400. This decorator activates commit on response. This way, if the view function
  401. runs successfully, a commit is made; if the viewfunc produces an exception,
  402. a rollback is made. This is one of the most common ways to do transaction
  403. control in Web apps.
  404. """
  405. warnings.warn("commit_on_success is deprecated in favor of atomic.",
  406. RemovedInDjango18Warning, stacklevel=2)
  407. def entering(using):
  408. enter_transaction_management(using=using)
  409. def exiting(exc_type, using):
  410. try:
  411. if exc_type is not None:
  412. if is_dirty(using=using):
  413. rollback(using=using)
  414. else:
  415. if is_dirty(using=using):
  416. try:
  417. commit(using=using)
  418. except:
  419. rollback(using=using)
  420. raise
  421. finally:
  422. leave_transaction_management(using=using)
  423. return _transaction_func(entering, exiting, using)
  424. def commit_manually(using=None):
  425. """
  426. Decorator that activates manual transaction control. It just disables
  427. automatic transaction control and doesn't do any commit/rollback of its
  428. own -- it's up to the user to call the commit and rollback functions
  429. themselves.
  430. """
  431. warnings.warn("commit_manually is deprecated in favor of set_autocommit.",
  432. RemovedInDjango18Warning, stacklevel=2)
  433. def entering(using):
  434. enter_transaction_management(using=using)
  435. def exiting(exc_type, using):
  436. leave_transaction_management(using=using)
  437. return _transaction_func(entering, exiting, using)
  438. def commit_on_success_unless_managed(using=None, savepoint=False):
  439. """
  440. Transitory API to preserve backwards-compatibility while refactoring.
  441. Once the legacy transaction management is fully deprecated, this should
  442. simply be replaced by atomic. Until then, it's necessary to guarantee that
  443. a commit occurs on exit, which atomic doesn't do when it's nested.
  444. Unlike atomic, savepoint defaults to False because that's closer to the
  445. legacy behavior.
  446. """
  447. connection = get_connection(using)
  448. if connection.get_autocommit() or connection.in_atomic_block:
  449. return atomic(using, savepoint)
  450. else:
  451. def entering(using):
  452. pass
  453. def exiting(exc_type, using):
  454. set_dirty(using=using)
  455. return _transaction_func(entering, exiting, using)