From f56a80c0024038b0f73e5edf4d056ba1437cf79b Mon Sep 17 00:00:00 2001 From: "Brian D. Caruso" Date: Mon, 26 Aug 2024 14:02:19 -0400 Subject: [PATCH] Change to flask sqlalchemy db session close. arxivce-1639 Removes unnecessary rollback on error. The existing call to Session.remove() would already do this if there was an open transaction. The Sqlalchemy docs say (sqlalchemy.scoping.scoped_session.close()) "This expunges all ORM objects associated with this :class:`_orm.Session`, ends any transaction in progress and :term:`releases` any :class:`_engine.Connection` objects which this :class:`_orm.Session` itself has checked out from associated :class:`_engine.Engine` objects. The operation then leaves the :class:`_orm.Session` in a state which it may be used again." --- arxiv/base/__init__.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/arxiv/base/__init__.py b/arxiv/base/__init__.py index 38f2f04f..6d068afe 100644 --- a/arxiv/base/__init__.py +++ b/arxiv/base/__init__.py @@ -125,6 +125,10 @@ def register_blueprint(self: Flask, blueprint: Blueprint, @app.teardown_appcontext def remove_scoped_session (response_or_exc: BaseException | None) -> None: - if response_or_exc: - Session.rollback() + """Cleans up the DB session. + + Will rollback any uncomitted transactions (via Session.remove() + which will call Sesion.close()). When Session is used again in this thread, + a new one will be created. + """ Session.remove()