MCPcopy
hub / github.com/django/django / __exit__

Method __exit__

django/db/transaction.py:235–324  ·  view source on GitHub ↗
(self, exc_type, exc_value, traceback)

Source from the content-addressed store, hash-verified

233 connection.atomic_blocks.append(self)
234
235 def __exit__(self, exc_type, exc_value, traceback):
236 connection = get_connection(self.using)
237
238 if connection.in_atomic_block:
239 connection.atomic_blocks.pop()
240
241 if connection.savepoint_ids:
242 sid = connection.savepoint_ids.pop()
243 else:
244 # Prematurely unset this flag to allow using commit or rollback.
245 connection.in_atomic_block = False
246
247 try:
248 if connection.closed_in_transaction:
249 # The database will perform a rollback by itself.
250 # Wait until we exit the outermost block.
251 pass
252
253 elif exc_type is None and not connection.needs_rollback:
254 if connection.in_atomic_block:
255 # Release savepoint if there is one
256 if sid is not None:
257 try:
258 connection.savepoint_commit(sid)
259 except DatabaseError:
260 try:
261 connection.savepoint_rollback(sid)
262 # The savepoint won't be reused. Release it to
263 # minimize overhead for the database server.
264 connection.savepoint_commit(sid)
265 except Error:
266 # If rolling back to a savepoint fails, mark
267 # for rollback at a higher level and avoid
268 # shadowing the original exception.
269 connection.needs_rollback = True
270 raise
271 else:
272 # Commit transaction
273 try:
274 connection.commit()
275 except DatabaseError:
276 try:
277 connection.rollback()
278 except Error:
279 # An error during rollback means that something
280 # went wrong with the connection. Drop it.
281 connection.close()
282 raise
283 else:
284 # This flag will be set to True again if there isn't a
285 # savepoint allowing to perform the rollback at this level.
286 connection.needs_rollback = False
287 if connection.in_atomic_block:
288 # Roll back to savepoint if there is one, mark for rollback
289 # otherwise.
290 if sid is None:
291 connection.needs_rollback = True
292 else:

Callers

nothing calls this directly

Calls 8

savepoint_commitMethod · 0.80
savepoint_rollbackMethod · 0.80
commitMethod · 0.80
rollbackMethod · 0.80
set_autocommitMethod · 0.80
get_connectionFunction · 0.70
popMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected