refactor: migrate manual Session usage to sessionmaker().begin() for better transaction management#32433
refactor: migrate manual Session usage to sessionmaker().begin() for better transaction management#32433Zainul342 wants to merge 3 commits intolanggenius:mainfrom
Conversation
…automated transaction management
Summary of ChangesHello @Zainul342, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly refactors the application's database session management by adopting SQLAlchemy's Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
The pull request refactors SQLAlchemy session management across multiple files, replacing direct Session(db.engine) instantiation with sessionmaker(db.engine).begin() context managers. This change streamlines transaction handling by implicitly committing on success and rolling back on errors, leading to the removal of explicit session.commit() calls. The review comment acknowledges this as a positive improvement for transaction management but suggests an optimization: instead of creating a new sessionmaker factory on every call, a single sessionmaker instance should be created globally and reused throughout the application to avoid performance overhead.
|
sorry for late review, can you make a smaller pr, like ~10 LoC ? we can do it little by little |
Hey maintainers,
I've implemented the migration from manual Session(db.engine) usage to the sessionmaker().begin() context manager as suggested in issue #24245.
Summary:
This PR refactors how local database sessions are handled across the API codebase. By moving to sessionmaker(bind=db.engine).begin(), we ensure that transactions are automatically
committed upon block exit and rolled back in case of exceptions, reducing the risk of manual commit errors or transaction leaks.
Changes:
Fixes #24245
Checklist: