Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a global SQLAlchemy engine for db connection pooling with psycopg2 #826

Open
qcdyx opened this issue Nov 15, 2024 · 1 comment · May be fixed by #837
Open

Use a global SQLAlchemy engine for db connection pooling with psycopg2 #826

qcdyx opened this issue Nov 15, 2024 · 1 comment · May be fixed by #837
Assignees

Comments

@qcdyx
Copy link
Contributor

qcdyx commented Nov 15, 2024

Refactor the code to use a shared engine upon open and close session per request, avoiding redundant engine/pool creation. A global engine is safe to share across threads because SQLAlchemy's Engine object is designed to be thread-safe. Using a global engine ensures that all database interactions reuse the same pool of connections without creating redundant engines.

@qcdyx qcdyx added Ready and removed Ready labels Nov 15, 2024
@qcdyx qcdyx self-assigned this Nov 18, 2024
@qcdyx
Copy link
Contributor Author

qcdyx commented Nov 19, 2024

The focus is not just on the global engine but also on managing session lifecycle per request. To achieve this, I applied a context manager to ensure sessions are properly closed after each request. This approach also aligns with my investigation in May into using FastAPI dependency injection with connection pooling for better session handling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant