[PR #32433] refactor: migrate manual Session usage to sessionmaker().begin() for better transaction management #33741

Open
opened 2026-02-21 20:53:48 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/32433

State: open
Merged: No


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:

  • Refactored 75+ files across api/controllers, api/services, api/tasks, and api/core.
  • Replaced with Session(db.engine) as session: with with sessionmaker(db.engine).begin() as session:.
  • Removed explicit session.commit() calls within the new context manager blocks to avoid redundant operations.
  • Maintained support for specific flags like expire_on_commit=False where they were previously used.
  • Verified that global Flask-SQLAlchemy db.session usage remains untouched as it follows a different lifecycle.

Fixes #24245

Checklist:

  • I understand that this PR may be closed in case there was no previous discussion or issues.
  • I've added a test for each change that was introduced (Verified through existing regression tests).
  • I've updated the documentation accordingly (No docs update needed for internal refactoring).
  • I ran make lint and make type-check (backend).
**Original Pull Request:** https://github.com/langgenius/dify/pull/32433 **State:** open **Merged:** No --- 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: - Refactored 75+ files across api/controllers, api/services, api/tasks, and api/core. - Replaced with Session(db.engine) as session: with with sessionmaker(db.engine).begin() as session:. - Removed explicit session.commit() calls within the new context manager blocks to avoid redundant operations. - Maintained support for specific flags like expire_on_commit=False where they were previously used. - Verified that global Flask-SQLAlchemy db.session usage remains untouched as it follows a different lifecycle. Fixes #24245 Checklist: - [x] I understand that this PR may be closed in case there was no previous discussion or issues. - [x] I've added a test for each change that was introduced (Verified through existing regression tests). - [x] I've updated the documentation accordingly (No docs update needed for internal refactoring). - [x] I ran make lint and make type-check (backend).
yindo added the pull-request label 2026-02-21 20:53:48 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#33741