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

#advisory_lock_exists?, #initialize, lock_stack, #lock_str, #query_cache_buster, #stable_hashcode, #with_advisory_lock_if_needed, #yield_with_lock

Constructor Details

This class inherits a constructor from WithAdvisoryLock::Base

Instance Method Details

#already_locked?Boolean

MySQL doesn’t support nested locks:

Returns:

  • (Boolean)


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

def already_locked?
  lock_stack.last == lock_str
end

#quoted_lock_strObject

MySQL wants a string as the lock key.



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

def quoted_lock_str
  connection.quote(lock_str)
end

#release_lockObject



18
19
20
21
22
23
24
25
# File 'lib/with_advisory_lock/mysql.rb', line 18

def release_lock
  # Returns > 0 if the lock was released,
  # 0 if the lock was not established by this thread
  # (in which case the lock is not released), and
  # NULL if the named lock did not exist.
  sql = "SELECT RELEASE_LOCK(#{quoted_lock_str}) #{query_cache_buster}"
  connection.select_value(sql).to_i > 0
end

#try_lockObject



4
5
6
7
8
9
10
11
12
13
14
15
16
# 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
  # Returns 1 if the lock was obtained successfully,
  # 0 if the attempt timed out (for example, because another client has
  # previously locked the name), or NULL if an error occurred
  # (such as running out of memory or the thread was killed with mysqladmin kill).
  sql = "SELECT GET_LOCK(#{quoted_lock_str}, 0) #{query_cache_buster}"
  connection.select_value(sql).to_i > 0
end