Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 403] [Versions 39 and 310]

This is a db record locking factory.

Copyright: Damyon Wiese 2013
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 233 lines (8 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class

db_record_lock_factory:: (10 methods):
  is_available()
  __construct()
  supports_timeout()
  supports_auto_release()
  supports_recursion()
  generate_unique_token()
  get_lock()
  release_lock()
  extend_lock()
  auto_release()


Class: db_record_lock_factory  - X-Ref

This is a db record locking factory.

This lock factory uses record locks relying on sql of the form "SET XXX where YYY" and checking if the
value was set. It supports timeouts, autorelease and can work on any DB. The downside - is this
will always be slower than some shared memory type locking function.

is_available()   X-Ref
Is available.

return: boolean - True if this lock type is available in this environment.

__construct($type)   X-Ref
Almighty constructor.

param: string $type - Used to prefix lock keys.

supports_timeout()   X-Ref
Return information about the blocking behaviour of the lock type on this platform.

return: boolean - True

supports_auto_release()   X-Ref
Will this lock type will be automatically released when a process ends.

return: boolean - True (shutdown handler)

supports_recursion()   X-Ref
Multiple locks for the same resource can be held by a single process.

return: boolean - False - not process specific.

generate_unique_token()   X-Ref
This function generates a unique token for the lock to use.
It is important that this token is not solely based on time as this could lead
to duplicates in a clustered environment (especially on VMs due to poor time precision).


get_lock($resource, $timeout, $maxlifetime = 86400)   X-Ref
Create and get a lock

param: string $resource - The identifier for the lock. Should use frankenstyle prefix.
param: int $timeout - The number of seconds to wait for a lock before giving up.
param: int $maxlifetime - Unused by this lock type.
return: boolean - true if a lock was obtained.

release_lock(lock $lock)   X-Ref
Release a lock that was previously obtained with @lock.

param: lock $lock - a lock obtained from this factory.
return: boolean - true if the lock is no longer held (including if it was never held).

extend_lock(lock $lock, $maxlifetime = 86400)   X-Ref
Extend a lock that was previously obtained with @lock.

param: lock $lock - a lock obtained from this factory.
param: int $maxlifetime - the new lifetime for the lock (in seconds).
return: boolean - true if the lock was extended.

auto_release()   X-Ref
Auto release any open locks on shutdown.
This is required, because we may be using persistent DB connections.