Skip to content

Fix/asyncsession to asyncsessionmaker#41

Open
abdelrahmanthelion142-a11y wants to merge 9 commits intoAeternalis-Ingenium:trunkfrom
abdelrahmanthelion142-a11y:fix/asyncsession-to-asyncsessionmaker
Open

Fix/asyncsession to asyncsessionmaker#41
abdelrahmanthelion142-a11y wants to merge 9 commits intoAeternalis-Ingenium:trunkfrom
abdelrahmanthelion142-a11y:fix/asyncsession-to-asyncsessionmaker

Conversation

@abdelrahmanthelion142-a11y
Copy link
Copy Markdown

Fix critical async session issue by using AsyncSessionMaker

Hi Nino Lindenberg! 👋

I’ve been using your repo and really appreciate the work you’ve done. I wanted to contribute a small improvement and help make the template more robust.

Summary

Previously, the template used a single AsyncSession instance for the entire application.
While the app may run normally on startup, this pattern can cause serious concurrency issues and shared state conflicts when handling multiple requests asynchronously.

This PR replaces the global session with AsyncSessionMaker, ensuring that each request or operation receives its own AsyncSession instance, which is the standard pattern for async SQLAlchemy + FastAPI.

Usage Example (from a video by Corey Schafer on converting from sync SQLAlchemy to async)

Screenshot 2026-03-27 002321

YouTube Video

Notes

  • There is no direct documentation for this pattern in FastAPI or SQLAlchemy because SQLAlchemy itself does not cover async integration specifically for FastAPI.
  • FastAPI examples often demonstrate similar patterns using SQLModel, which is almost the same as in SQLAlchemy, but I wanted the reference to be with SQLAlchemy.
  • This change is fully backward compatible.
  • I’ve customized your repo for this improvement, and there could be more changes coming up — for example, setting up a Docker Postgres database as a test database. The goal is just to help and improve the template for everyone using it. 🤝

@abdelrahmanthelion142-a11y
Copy link
Copy Markdown
Author

there was some issues with some typos in Dockerfile and README.MD that caused the pre-commit checks to fail but i fixed them

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant