Sha256: 0b780ba8a32f319252b1df2e4c6ec8e006adb85e9ccedfc357606ebcac1a6fd1

Contents?: true

Size: 1.07 KB

Versions: 3

Compression:

Stored size: 1.07 KB

Contents

module WithAdvisoryLock
  class MySQL < Base
    # See http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_get-lock
    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

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

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

    # MySQL doesn't support nested locks:
    def already_locked?
      lock_stack.last == lock_stack_item
    end

    # MySQL wants a string as the lock key.
    def quoted_lock_str
      connection.quote(lock_str)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
with_advisory_lock-3.2.0 lib/with_advisory_lock/mysql.rb
with_advisory_lock-3.1.1 lib/with_advisory_lock/mysql.rb
with_advisory_lock-3.1.0 lib/with_advisory_lock/mysql.rb