Last active
November 26, 2019 08:57
-
-
Save hoverlover/a16ff56c8f7617da7db9 to your computer and use it in GitHub Desktop.
PG::LockNotAvailable rescue woes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def save_with_lock | |
r = Record.first | |
# This will raise PG::LockNotAvailable if already locked | |
# | |
r.with_lock("FOR UPDATE NOWAIT") do | |
# do stuff | |
end | |
# This rescue block isn't executed for some reason?? | |
# | |
rescue PG::LockNotAvailable | |
puts "lock not available" | |
# But this one works | |
# | |
rescue ActiveRecord::StatementInvalid => e | |
if e.message.include? "LockNotAvailable" | |
puts "lock not available" | |
else | |
raise | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@sethsweepnovu I've always wondering, why not just using
rescue ActiveRecord::LockWaitTimeout
without care about the bubbled error?@ktravers
PG::LockNotAvailable
It is a parent error, the current error beingActiveRecord::LockWaitTimeout
. Hence you can find it ine.cause