mirror of
https://github.com/zulip/zulip.git
synced 2025-11-04 22:13:26 +00:00
Fixes #2665. Regenerated by tabbott with `lint --fix` after a rebase and change in parameters. Note from tabbott: In a few cases, this converts technical debt in the form of unsorted imports into different technical debt in the form of our largest files having very long, ugly import sequences at the start. I expect this change will increase pressure for us to split those files, which isn't a bad thing. Signed-off-by: Anders Kaseorg <anders@zulip.com>
41 lines
1.6 KiB
Python
41 lines
1.6 KiB
Python
from typing import Any, Optional
|
|
|
|
import sqlalchemy
|
|
from django.db import connection
|
|
|
|
from zerver.lib.db import TimeTrackingConnection
|
|
|
|
|
|
# This is a Pool that doesn't close connections. Therefore it can be used with
|
|
# existing Django database connections.
|
|
class NonClosingPool(sqlalchemy.pool.NullPool):
|
|
def status(self) -> str:
|
|
return "NonClosingPool"
|
|
|
|
def _do_return_conn(self, conn: sqlalchemy.engine.base.Connection) -> None:
|
|
pass
|
|
|
|
def recreate(self) -> 'NonClosingPool':
|
|
return self.__class__(creator=self._creator,
|
|
recycle=self._recycle,
|
|
use_threadlocal=self._use_threadlocal,
|
|
reset_on_return=self._reset_on_return,
|
|
echo=self.echo,
|
|
logging_name=self._orig_logging_name,
|
|
_dispatch=self.dispatch)
|
|
|
|
sqlalchemy_engine: Optional[Any] = None
|
|
def get_sqlalchemy_connection() -> sqlalchemy.engine.base.Connection:
|
|
global sqlalchemy_engine
|
|
if sqlalchemy_engine is None:
|
|
def get_dj_conn() -> TimeTrackingConnection:
|
|
connection.ensure_connection()
|
|
return connection.connection
|
|
sqlalchemy_engine = sqlalchemy.create_engine('postgresql://',
|
|
creator=get_dj_conn,
|
|
poolclass=NonClosingPool,
|
|
pool_reset_on_return=False)
|
|
sa_connection = sqlalchemy_engine.connect()
|
|
sa_connection.execution_options(autocommit=False)
|
|
return sa_connection
|