Skip to content

Commit

Permalink
Merge pull request #24 from alan-turing-institute/23-fix-sqlalchemy-c…
Browse files Browse the repository at this point in the history
…onnection-pool-problems

Fix sqlalchemy connection pool problems
  • Loading branch information
jemrobinson authored Jan 9, 2025
2 parents 9225d0d + b71f253 commit 958eb0d
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions guacamole_user_sync/postgresql/postgresql_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ def engine(self) -> Engine:
self._engine = create_engine(url_object, echo=False)
return self._engine

def session(self) -> Session:
def session(self, *, expire_on_commit: bool = True) -> Session:
if self._session:
return self._session
return Session(self.engine)
return Session(self.engine, expire_on_commit=expire_on_commit)

def add_all(self, items: list[T]) -> None:
with self.session() as session, session.begin():
Expand Down Expand Up @@ -84,9 +84,10 @@ def query(
table: type[T],
**filter_kwargs: Any, # noqa: ANN401
) -> list[T]:
with self.session() as session, session.begin():
# We need expire_on_commit to ensure that the results are not marked as stale
with self.session(expire_on_commit=False) as session, session.begin():
if filter_kwargs:
result = session.query(table).filter_by(**filter_kwargs)
result = session.query(table).filter_by(**filter_kwargs).all()
else:
result = session.query(table)
result = session.query(table).all()
return list(result)

0 comments on commit 958eb0d

Please sign in to comment.