Class: WithAdvisoryLock::MySQL

Inherits:
Base
  • Object
show all
Defined in:
lib/with_advisory_lock/mysql.rb

Instance Attribute Summary

Attributes inherited from Base

#connection, #lock_name, #timeout_seconds

Instance Method Summary collapse

Methods inherited from Base

#initialize, lock_stack, #lock_str, #stable_hashcode, #unique_column_name, #with_advisory_lock_if_needed, #yield_with_lock, #yield_with_lock_and_timeout

Constructor Details

This class inherits a constructor from WithAdvisoryLock::Base

Instance Method Details

#already_locked?Boolean

MySQL doesn't support nested locks:

Returns:

  • (Boolean)

23
24
25
# File 'lib/with_advisory_lock/mysql.rb', line 23

def already_locked?
  lock_stack.last == lock_str
end

#execute_successful?(mysql_function) ⇒ Boolean

Returns:

  • (Boolean)

17
18
19
20
# File 'lib/with_advisory_lock/mysql.rb', line 17

def execute_successful?(mysql_function)
  sql = "SELECT #{mysql_function} AS #{unique_column_name}"
  connection.select_value(sql).to_i > 0
end

#quoted_lock_strObject

MySQL wants a string as the lock key.


28
29
30
# File 'lib/with_advisory_lock/mysql.rb', line 28

def quoted_lock_str
  connection.quote(lock_str)
end

#release_lockObject


13
14
15
# File 'lib/with_advisory_lock/mysql.rb', line 13

def release_lock
  execute_successful?("RELEASE_LOCK(#{quoted_lock_str})")
end

#try_lockObject


4
5
6
7
8
9
10
11
# File 'lib/with_advisory_lock/mysql.rb', line 4

def try_lock
  unless lock_stack.empty?
    raise NestedAdvisoryLockError.new(
      "MySQL doesn't support nested Advisory Locks",
      lock_stack.dup)
  end
  execute_successful?("GET_LOCK(#{quoted_lock_str}, 0)")
end