How to clear database sessions pool after making request

Viewed 22

I'm trying to use FastAPI with async SQLAlchemy. For db session I want to create FastApi dependency:

async_db_url = f'postgresql+asyncpg://{db_user}:{db_pass}@{db_host}:{db_port}/{db_name}'
async_engine = create_async_engine(async_db_url)
async_session = sessionmaker(async_engine, expire_on_commit=False, class_=AsyncSession)


async def async_db_session() -> AsyncGenerator:
    async with async_session() as session:
        try:
            yield session
        except Exception as exc:
            await session.rollback()
            raise exc

I expect that the session will be closed after use, because of context manager. But it does not happen. Instead of that I can see new connections with each request. And number of connections riches the limit, I can't connect to db anymore.

I tried to use engine.dispose() from documentation, but it caused very slow requests. I did it like that:

async def async_db_session() -> AsyncGenerator:
    async with async_session() as session:
        try:
            yield session
        except Exception as exc:
            await session.rollback()
            raise exc
        finally:
            await engine.dispose()

How to force connection to drop after making a request?

0 Answers
Related