mirror of
https://github.com/zulip/zulip.git
synced 2025-11-06 15:03:34 +00:00
sqlalchemy_utils: Make get_sqlalchemy_connection a context manager.
Although our NonClosingPool prevents the SQLAlchemy connection from closing the underlying Django connection, we still want to properly dispose of the associated SQLAlchemy structures. Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
committed by
Tim Abbott
parent
8e5ae4e829
commit
29330c180a
@@ -1,7 +1,9 @@
|
||||
from typing import Any, Optional
|
||||
from contextlib import contextmanager
|
||||
from typing import Iterator, Optional
|
||||
|
||||
import sqlalchemy
|
||||
from django.db import connection
|
||||
from sqlalchemy.engine import Connection, Engine
|
||||
|
||||
from zerver.lib.db import TimeTrackingConnection
|
||||
|
||||
@@ -26,10 +28,11 @@ class NonClosingPool(sqlalchemy.pool.NullPool):
|
||||
)
|
||||
|
||||
|
||||
sqlalchemy_engine: Optional[Any] = None
|
||||
sqlalchemy_engine: Optional[Engine] = None
|
||||
|
||||
|
||||
def get_sqlalchemy_connection() -> sqlalchemy.engine.base.Connection:
|
||||
@contextmanager
|
||||
def get_sqlalchemy_connection() -> Iterator[Connection]:
|
||||
global sqlalchemy_engine
|
||||
if sqlalchemy_engine is None:
|
||||
|
||||
@@ -43,6 +46,5 @@ def get_sqlalchemy_connection() -> sqlalchemy.engine.base.Connection:
|
||||
poolclass=NonClosingPool,
|
||||
pool_reset_on_return=None,
|
||||
)
|
||||
sa_connection = sqlalchemy_engine.connect()
|
||||
sa_connection.execution_options(autocommit=False)
|
||||
return sa_connection
|
||||
with sqlalchemy_engine.connect().execution_options(autocommit=False) as sa_connection:
|
||||
yield sa_connection
|
||||
|
||||
Reference in New Issue
Block a user