79318787

Date: 2024-12-30 22:52:49
Score: 1.5
Natty:
Report link

When a Session is created a connection resource is requested from the Engine. The connection remains open until the transaction completes, which can happen when a rollback or commit is called. In the case of autocommit, the commit occurs immediately after a statement is processed. At this point the transaction ends and the underlying connection resource is returned back to the connection pool.

Based on my understanding of how SQLAlchemy manages its connection pools, it seems safe to not explicitly close sessions. GC would clean up any Session objects that were no longer referenced. But there's no advantage to keeping sessions alive, that I'm aware of, and best practice is normally to close resources when they're no longer needed.

Reasons:
  • Long answer (-0.5):
  • No code block (0.5):
  • Starts with a question (0.5): When a
  • Low reputation (1):
Posted by: Pebble94464