Closed brauliobo closed 2 years ago
Using a transaction and xact_lock solves the issue
DB.transaction do
DB.select(Sequel.function :pg_try_advisory_xact_lock, 0, 123).all
# do work
end
Alternatively, DB.synchronize{ }
can be used. https://www.rubydoc.info/github/jeremyevans/sequel/Sequel%2FDatabase:synchronize
Take the code below that relies on postgresql advisory locks to execute in parallel with multiple workers and threads:
Each worker relies on a database pool. I need to force Sequel to use the same connection to unlock as the connection use to acquire the lock, otherwise the unlock fails