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)