Skip to content

Commit

Permalink
Use an explicit transaction when registering listeners
Browse files Browse the repository at this point in the history
See #310 — sqlalchemy-2.0 migration
  • Loading branch information
sde1000 committed Jan 25, 2025
1 parent 8708ef4 commit c9e0ebc
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions quicktill/listen.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,19 @@ def update_listening_channels(self):

to_add = wanted - self._db_listening
to_remove = self._db_listening - wanted
for channel in to_add:
log.debug("listen for %s", channel)
self.connection.execute(
text(f"LISTEN {channel};")
.execution_options(autocommit=True))
self._db_listening.add(channel)
for channel in to_remove:
log.debug("stop listening for %s", channel)
self.connection.execute(
text(f"UNLISTEN {channel};")
.execution_options(autocommit=True))
self._db_listening.discard(channel)
with self.connection.begin():
for channel in to_add:
log.debug("listen for %s", channel)
self.connection.execute(
text(f"LISTEN {channel};")
.execution_options(autocommit=True))
self._db_listening.add(channel)
for channel in to_remove:
log.debug("stop listening for %s", channel)
self.connection.execute(
text(f"UNLISTEN {channel};")
.execution_options(autocommit=True))
self._db_listening.discard(channel)
if not self._db_listening:
# We're not listening for anything any more - close the connection
log.debug("close database connection")
Expand Down

0 comments on commit c9e0ebc

Please sign in to comment.