From fbab16348cc49bac27715a1cce689b9d723e4ecf Mon Sep 17 00:00:00 2001 From: Daniel Naves de Carvalho Date: Thu, 27 Aug 2015 15:49:53 -0300 Subject: [PATCH] Dealing with open transactions when using advisory lock. Issue #14 --- lib/with_advisory_lock/postgresql.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/with_advisory_lock/postgresql.rb b/lib/with_advisory_lock/postgresql.rb index 0740a62..4919e32 100644 --- a/lib/with_advisory_lock/postgresql.rb +++ b/lib/with_advisory_lock/postgresql.rb @@ -2,11 +2,17 @@ module WithAdvisoryLock class PostgreSQL < Base # See http://www.postgresql.org/docs/9.1/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS def try_lock - execute_successful?('pg_try_advisory_lock') + if connection.open_transactions > 0 + execute_successful?('pg_try_advisory_xact_lock') + else + execute_successful?('pg_try_advisory_lock') + end end def release_lock - execute_successful?('pg_advisory_unlock') + if connection.open_transactions <= 0 + execute_successful?('pg_advisory_unlock') + end end def execute_successful?(pg_function)