MCPcopy
hub / github.com/django/django / __enter__

Method __enter__

django/db/transaction.py:193–233  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

191 self._from_testcase = False
192
193 def __enter__(self):
194 connection = get_connection(self.using)
195
196 if (
197 self.durable
198 and connection.atomic_blocks
199 and not connection.atomic_blocks[-1]._from_testcase
200 ):
201 raise RuntimeError(
202 "A durable atomic block cannot be nested within another "
203 "atomic block."
204 )
205 if not connection.in_atomic_block:
206 # Reset state when entering an outermost atomic block.
207 connection.commit_on_exit = True
208 connection.needs_rollback = False
209 if not connection.get_autocommit():
210 # Pretend we're already in an atomic block to bypass the code
211 # that disables autocommit to enter a transaction, and make a
212 # note to deal with this case in __exit__.
213 connection.in_atomic_block = True
214 connection.commit_on_exit = False
215
216 if connection.in_atomic_block:
217 # We're already in a transaction; create a savepoint, unless we
218 # were told not to or we're already waiting for a rollback. The
219 # second condition avoids creating useless savepoints and prevents
220 # overwriting needs_rollback until the rollback is performed.
221 if self.savepoint and not connection.needs_rollback:
222 sid = connection.savepoint()
223 connection.savepoint_ids.append(sid)
224 else:
225 connection.savepoint_ids.append(None)
226 else:
227 connection.set_autocommit(
228 False, force_begin_transaction_with_broken_autocommit=True
229 )
230 connection.in_atomic_block = True
231
232 if connection.in_atomic_block:
233 connection.atomic_blocks.append(self)
234
235 def __exit__(self, exc_type, exc_value, traceback):
236 connection = get_connection(self.using)

Callers

nothing calls this directly

Calls 5

get_autocommitMethod · 0.80
savepointMethod · 0.80
set_autocommitMethod · 0.80
get_connectionFunction · 0.70
appendMethod · 0.45

Tested by

no test coverage detected