Skip to content

Commit 07a82ac

Browse files
committed
work in progress
1 parent 073c271 commit 07a82ac

File tree

3 files changed

+125
-5
lines changed

3 files changed

+125
-5
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from django.db import transaction
2+
from sample_mflix.models import Movie
3+
4+
# start-transaction-decorator
5+
@transaction.atomic
6+
def run_movie_transaction():
7+
Movie.objects.create(
8+
title="Poor Things",
9+
runtime=141,
10+
genres=["Comedy", "Romance"]
11+
)
12+
# end-transaction-decorator
13+
14+
# start-transaction-manager
15+
def run_movie_transaction():
16+
with transaction.atomic():
17+
Movie.objects.create(
18+
title="Poor Things",
19+
runtime=141,
20+
genres=["Comedy", "Romance"]
21+
)
22+
# end-transaction-manager

source/interact-data/transactions.txt

Lines changed: 92 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,97 @@ Transactions and Sessions
2020
Overview
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
2525
that 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
2727
transaction 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.

source/limitations-upcoming.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ Query Support
183183
the :ref:`raw_aggregate() method <django-raw-queries-search>`.
184184
- ✓
185185

186+
.. _django-feature-compat-db-coll:
187+
186188
Database and Collection Support
187189
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
188190

@@ -248,7 +250,15 @@ Database and Collection Support
248250
- ✓
249251

250252
* - Transactions
251-
- *Unsupported*.
253+
- *Partially Supported*. You can use {+framework+}'s transactions API with the
254+
following limitations:
255+
256+
- ``QuerySet.union()`` is not supported within a transaction.
257+
- If a transaction generates an error, the transaction is no longer usable.
258+
- Nested atomic blocks are not supported. The outermost atomic block starts
259+
a transaction, and any subsequent atomic blocks have no effect.
260+
- Migration operations do not run inside a transaction, which differs from {+framework+}'s
261+
default migration behavior.
252262
- ✓
253263

254264
Django Features

0 commit comments

Comments
 (0)