@@ -20,9 +20,97 @@ Transactions and Sessions
2020Overview
2121--------
2222
23- In this guide, you can learn how to use the {+django-odm+} to perform
24- **transactions**. Transactions allow you to perform a series of operations
23+ In this guide, you can learn how to use {+django-odm+} to perform
24+ **transactions**. Transactions allow you to run a series of operations
2525that change data only if the entire transaction is committed.
26- If any operation in the transaction does not succeed, the library stops the
26+ If any operation in the transaction does not succeed, {+django-odm+} stops the
2727transaction and discards all data changes before they ever become
28- visible. This feature is called **atomicity**.
28+ visible. This feature is called **atomicity**.
29+
30+ In MongoDB, transactions run within logical sessions. A
31+ session is a grouping of related read or write operations that you
32+ want to run sequentially. Sessions enable causal consistency for a group
33+ of operations and allow you to run operations in an **ACID-compliant**
34+ transaction, which is a transaction that meets an expectation of
35+ atomicity, consistency, isolation, and durability.
36+
37+ You can use {+framework+}'s transaction API to perform database transactions.
38+ To run operations within a transaction, define them inside an atomic block of
39+ code. {+framework+} manages session logic internally, so you do not need to
40+ manually start a session before running a transaction.
41+
42+ .. important:: Transaction Limitations
43+
44+ {+django-odm+}'s support for the {+framework+} transaction API
45+ has several limitations. To view a list of limitations, see
46+ :ref:`Database and Collection Support <django-feature-compat-db-coll>`
47+ in the Django and MongoDB Feature Compatibility guide.
48+
49+ Sample Data
50+ ~~~~~~~~~~~
51+
52+ The examples in this guide use the ``Movie`` model, which represents
53+ the ``sample_mflix.movies`` collection from the :atlas:`Atlas sample datasets </sample-data>`.
54+ The ``Movie`` model class has the following definition:
55+
56+ .. literalinclude:: /includes/interact-data/crud.py
57+ :start-after: start-models
58+ :end-before: end-models
59+ :language: python
60+ :copyable:
61+
62+ .. include:: /includes/use-sample-data.rst
63+
64+ .. replacement:: model-classes
65+
66+ ``Movie`` model includes
67+
68+ .. replacement:: model-imports
69+
70+ .. code-block:: python
71+
72+ from <your application name>.models import Movie
73+ from django.utils import timezone
74+ from datetime import datetime
75+
76+ Start a Transaction
77+ -------------------
78+
79+ To start a database transaction, define an atomic block of code
80+ by adding the ``@transaction.atomic`` decorator above your function.
81+ This decorator guarantees the atomicity of any database operations
82+ within the function. If the function successfully completes, the
83+ changes are committed MongoDB.
84+
85+ The following example calls the ``create()`` method within a transaction,
86+ inserting a document into the ``sample_mflix.movies`` collection:
87+
88+ .. literalinclude:: /includes/interact-data/transactions.py
89+ :start-after: start-transaction-decorator
90+ :end-before: end-transaction-decorator
91+ :language: python
92+ :copyable:
93+
94+ Alternatively, you can use the ``transaction.atomic()`` context manager
95+ to create an atomic block. This example runs the same operation as the
96+ previous example but uses a context manager to start a transaction:
97+
98+ .. literalinclude:: /includes/interact-data/transactions.py
99+ :start-after: start-transaction-manager
100+ :end-before: end-transaction-manager
101+ :language: python
102+ :copyable:
103+
104+ Handle Transaction Errors
105+ -------------------------
106+
107+ To handle exceptions that occur during a transaction, add error handling
108+ logic around your atomic code block. If you include error handing logic inside
109+ the atomic block, {+framework+} might ignore these errors, resulting in unexpected
110+ behavior.
111+
112+ Additional Information
113+ ----------------------
114+
115+ To learn more about the {+framework+} transaction API, see `Database Transactions
116+ <{+django-docs+}/topics/db/transactions>`__ in the {+framework+} documentation.
0 commit comments