MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / close

Method close

lib/sqlalchemy/orm/session.py:1421–1450  ·  view source on GitHub ↗
(self, invalidate: bool = False)

Source from the content-addressed store, hash-verified

1419 _StateChangeStates.ANY, SessionTransactionState.CLOSED
1420 )
1421 def close(self, invalidate: bool = False) -> None:
1422 if self.nested:
1423 self.session._nested_transaction = (
1424 self._previous_nested_transaction
1425 )
1426
1427 self.session._transaction = self._parent
1428
1429 for connection, transaction, should_commit, autoclose in set(
1430 self._connections.values()
1431 ):
1432 if invalidate and self._parent is None:
1433 connection.invalidate()
1434 if should_commit and transaction.is_active:
1435 transaction.close()
1436 if autoclose and self._parent is None:
1437 connection.close()
1438
1439 self._state = SessionTransactionState.CLOSED
1440 sess = self.session
1441
1442 # TODO: these two None sets were historically after the
1443 # event hook below, and in 2.0 I changed it this way for some reason,
1444 # and I remember there being a reason, but not what it was.
1445 # Why do we need to get rid of them at all? test_memusage::CycleTest
1446 # passes with these commented out.
1447 # self.session = None # type: ignore
1448 # self._connections = None # type: ignore
1449
1450 sess.dispatch.after_transaction_end(sess, self)
1451
1452 def _get_subject(self) -> Session:
1453 return self.session

Callers 2

commitMethod · 0.95
rollbackMethod · 0.95

Calls 4

after_transaction_endMethod · 0.80
valuesMethod · 0.45
invalidateMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected