Closed meteoDaniel closed 1 month ago
In my database, I can see numerous idle sessions that caused an outage. No more clients were allowed in my PostgreSQL
Sessions will be closed correctly
I recommend to use a contextmanager to handle database sessions:
@contextmanager def get_scoped_session(database: str) -> Generator[Session, None, None]: """ Returns a scoped sqlalchemy session for the specified database Args: database: database name Returns: scoped session object """ session = sessionmaker(bind=create_engine(database))() try: yield session session.commit() except Exception: session.rollback() raise finally: session.close() # Usage: with get_scoped_session(engine): # do stuff
Describe the bug
In my database, I can see numerous idle sessions that caused an outage. No more clients were allowed in my PostgreSQL
To Reproduce
Expected behavior
Sessions will be closed correctly
Configuration
Additional context
I recommend to use a contextmanager to handle database sessions: