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, #shared, #timeout_seconds, #transaction

Instance Method Summary collapse

Methods inherited from Base

#initialize, lock_stack, #lock_stack_item, #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)


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

def already_locked?
  lock_stack.last == lock_stack_item
end

#execute_successful?(mysql_function) ⇒ Boolean

Returns:

  • (Boolean)


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

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.



34
35
36
# File 'lib/with_advisory_lock/mysql.rb', line 34

def quoted_lock_str
  connection.quote(lock_str)
end

#release_lockObject



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

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

#try_lockObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 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
  if shared
    raise ArgumentError, 'shared locks are not supported on MySQL'
  end
  if transaction
    raise ArgumentError, 'transaction level locks are not supported on MySQL'
  end
  execute_successful?("GET_LOCK(#{quoted_lock_str}, 0)")
end