Class: WithAdvisoryLock::MySQL

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

Instance Attribute Summary

Attributes inherited from Base

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

Instance Method Summary collapse

Methods inherited from Base

#already_locked?, #initialize, #lock_and_yield, 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.positive?
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)


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

def try_lock
  raise ArgumentError, 'shared locks are not supported on MySQL' if shared
  raise ArgumentError, 'transaction level locks are not supported on MySQL' if transaction

  execute_successful?("GET_LOCK(#{quoted_lock_str}, 0)")
end