Skip to content

Commit 72b93db

Browse files
committed
feedback
1 parent 90d24a8 commit 72b93db

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

source/includes/interact-data/transactions.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,21 @@ def insert_movie_with_callback():
3434
runtime=140,
3535
genres=["Horror", "Comedy"]
3636
)
37-
3837
transaction.on_commit(get_horror_comedies)
3938
# end-callback
4039

4140
# start-handle-errors
42-
movie = Movie.objects.get(
43-
title="Jurassic Park",
44-
released=timezone.make_aware(datetime(1993, 6, 11))
45-
)
41+
movie = Movie.objects.get(title="Jurassic Park")
42+
movie.title = "Jurassic Park I"
4643
try:
4744
with transaction.atomic():
48-
movie.update(title="Jurassic Park I")
45+
movie.save()
4946
except DatabaseError:
50-
movie.update(title="Jurassic Park")
47+
movie.title = "Jurassic Park"
48+
49+
if movie.title == "Jurassic Park I":
50+
movie.plot = "An industrialist invites experts to visit his theme park of cloned dinosaurs. After a power failure," \
51+
" the creatures run loose, putting everyone's lives, including his grandchildren's, in danger."
52+
movie.save()
53+
5154
# end-handle-errors

source/interact-data/transactions.txt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ In this guide, you can learn how to use {+django-odm+} to perform
2424
**transactions**. Transactions allow you to run a series of operations
2525
that change data only if the entire transaction is committed.
2626
If any operation in the transaction does not succeed, {+django-odm+} stops the
27-
transaction and discards all data changes before they ever become
27+
transaction and discards all changes to the data before they ever become
2828
visible. This feature is called **atomicity**.
2929

3030
In MongoDB, transactions run within logical sessions. A
@@ -80,10 +80,10 @@ To start a database transaction, define an atomic block of code
8080
by adding the ``@transaction.atomic`` decorator above your function.
8181
This decorator guarantees the atomicity of any database operations
8282
within the function. If the function successfully completes, the
83-
changes are committed MongoDB.
83+
changes are committed to MongoDB.
8484

8585
The following example calls the ``create()`` method within a transaction,
86-
inserting a document into the ``sample_mflix.movies`` collection if the
86+
which inserts a document into the ``sample_mflix.movies`` collection if the
8787
transaction succeeds:
8888

8989
.. literalinclude:: /includes/interact-data/transactions.py
@@ -105,8 +105,8 @@ preceding example but uses a context manager to start a transaction:
105105
Run Callbacks After a Transaction
106106
---------------------------------
107107

108-
If you want to perform certain actions only if a transaction successfully
109-
completes, you can use the ``transaction.on_commit()`` method. This method allows you to
108+
To perform certain actions only if a transaction successfully completes,
109+
you can use the ``transaction.on_commit()`` function. This function allows you to
110110
register callbacks that run after a transaction is committed to the
111111
database. Pass a function, or any callable object, as an argument to
112112
``on_commit()``.
@@ -124,11 +124,12 @@ Handle Transaction Errors
124124
-------------------------
125125

126126
To handle exceptions that occur during a transaction, add error handling
127-
logic around your atomic code block. If you include error handing logic inside
128-
the atomic block, {+framework+} might ignore these errors, resulting in unexpected
129-
behavior.
127+
logic around your atomic code block. If you handle errors inside
128+
the atomic block, you might obscure these errors from {+framework+}. Since
129+
{+framework+} uses errors to determine whether to commit or roll
130+
back a transaction, this can cause unexpected behavior.
130131

131-
If a transaction does not succeed, your application does not revert any changes made
132+
If a transaction does not succeed, Django does not revert any changes made
132133
to a model's fields. To avoid inconsistencies between your models and database documents,
133134
you might need to manually restore the original field values.
134135

source/limitations-upcoming.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,7 @@ Database and Collection Support
257257
- If a transaction generates an error, the transaction is no longer usable.
258258
- Savepoints, or nested atomic blocks, are not supported. The outermost atomic block starts
259259
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.
260+
- Migration operations do not run inside a transaction.
262261
- Your MongoDB deployment must be a replica set or sharded cluster.
263262
- *Partially Supported*.
264263

0 commit comments

Comments
 (0)