Class: WithAdvisoryLock::MySQL

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

Overview

MySQL > 5.7.5 supports nested locks

Direct Known Subclasses

MySQLNoNesting

Instance Attribute Summary

Attributes inherited from Base

#connection, #lock_name, #shared, #timeout_seconds, #transaction

Instance Method Summary collapse

Methods inherited from Base

#already_locked?, #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

#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.



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

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

Raises:

  • (ArgumentError)


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

def try_lock
  raise ArgumentError, 'shared locks are not supported on MySQL' if shared
  if transaction
    raise ArgumentError, 'transaction level locks are not supported on MySQL'
  end
  execute_successful?("GET_LOCK(#{quoted_lock_str}, 0)")
end