@@ -68,12 +68,12 @@ def __enter__(self):
6868 # Pretend we're already in an atomic block to bypass the code
6969 # that disables autocommit to enter a transaction, and make a
7070 # note to deal with this case in __exit__.
71- connection .in_atomic_block_mongo = True
72- connection .commit_on_exit = False
71+ # connection.in_atomic_block_mongo = True
72+ # connection.commit_on_exit = False
7373
7474 if connection .in_atomic_block_mongo :
75- # We're already in a transaction
76- pass
75+ # We're already in a transaction. Increment the number of nested atomics.
76+ connection . nested_atomics += 1
7777 else :
7878 connection ._start_transaction (
7979 False , force_begin_transaction_with_broken_autocommit = True
@@ -89,25 +89,23 @@ def __exit__(self, exc_type, exc_value, traceback):
8989 if connection .in_atomic_block_mongo :
9090 connection .atomic_blocks_mongo .pop ()
9191
92- # Prematurely unset this flag to allow using commit or rollback.
93- connection ._in_atomic_block = False
92+ if connection .nested_atomics :
93+ connection .nested_atomics -= 1
94+ else :
95+ # Prematurely unset this flag to allow using commit or rollback.
96+ connection .in_atomic_block_mongo = False
9497 try :
95- if connection .closed_in_transaction :
96- # The database will perform a rollback by itself.
97- # Wait until we exit the outermost block.
98- pass
99-
100- elif exc_type is None and not connection .needs_rollback_mongo :
101- if connection ._in_atomic_block :
98+ if exc_type is None and not connection .needs_rollback_mongo :
99+ if connection .in_atomic_block_mongo :
102100 # Release savepoint if there is one
103101 pass
104102 else :
105103 # Commit transaction
106104 try :
107- connection ._commit_transaction ()
105+ connection .commit_mongo ()
108106 except DatabaseError :
109107 try :
110- connection ._rollback_transaction ()
108+ connection .rollback_mongo ()
111109 except Error :
112110 # An error during rollback means that something
113111 # went wrong with the connection. Drop it.
@@ -123,24 +121,19 @@ def __exit__(self, exc_type, exc_value, traceback):
123121 else :
124122 # Roll back transaction
125123 try :
126- connection ._rollback_transaction ()
124+ connection .rollback_mongo ()
127125 except Error :
128126 # An error during rollback means that something
129127 # went wrong with the connection. Drop it.
130128 connection .close ()
131129 finally :
132130 # Outermost block exit when autocommit was enabled.
133131 if not connection .in_atomic_block_mongo :
134- if connection .closed_in_transaction :
135- connection .connection = None
136- # else:
137- # connection.set_autocommit(True)
132+ pass
133+ # connection.set_autocommit(True)
138134 # Outermost block exit when autocommit was disabled.
139135 elif not connection .commit_on_exit :
140- if connection .closed_in_transaction :
141- connection .connection = None
142- else :
143- connection .in_atomic_block_mongo = False
136+ connection .in_atomic_block_mongo = False
144137
145138
146139def atomic (using = None , durable = False ):
0 commit comments