From ff4e8f63aa733cb1d2dfee3241caf73b3aab3a87 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Tue, 31 Oct 2023 15:05:40 +0000 Subject: [PATCH] Add statement timeout --- matcher/database.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/matcher/database.py b/matcher/database.py index e2e7398..46806da 100644 --- a/matcher/database.py +++ b/matcher/database.py @@ -8,6 +8,8 @@ from sqlalchemy.orm import scoped_session, sessionmaker session: sqlalchemy.orm.scoping.scoped_session = scoped_session(sessionmaker()) +timeout = 20_000 # 20 seconds + def init_db(db_url: str, echo: bool = False) -> None: """Initialise database.""" @@ -16,7 +18,14 @@ def init_db(db_url: str, echo: bool = False) -> None: def get_engine(db_url: str, echo: bool = False) -> sqlalchemy.engine.base.Engine: """Create an engine objcet.""" - return create_engine(db_url, pool_recycle=3600, echo=echo) + return create_engine( + db_url, + pool_recycle=3600, + echo=echo, + connect_args={ + "options": f"-c lock_timeout={timeout} -c statement_timeout={timeout}" + }, + ) def get_tables() -> list[str]: