Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sqlalchemy connection pool problems #24

Merged
merged 2 commits into from
Jan 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading