2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
22 * MySQL / MariaDB locking factory.
26 * @copyright Brendan Heywood <brendan@catalyst-au.net>
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 class mysql_lock_factory
implements lock_factory
{
31 /** @var string $dbprefix - used as a namespace for these types of locks */
32 protected $dbprefix = '';
34 /** @var \moodle_database $db Hold a reference to the global $DB */
37 /** @var array $openlocks - List of held locks - used by auto-release */
38 protected $openlocks = [];
41 * Return a unique prefix based on the database name and prefix.
42 * @param string $type - Used to prefix lock keys.
45 protected function get_unique_db_prefix($type) {
47 $prefix = $CFG->dbname
. ':';
48 if (isset($CFG->prefix
)) {
49 $prefix .= $CFG->prefix
;
51 $prefix .= '_' . $type . '_';
57 * @param string $type - Used to prefix lock keys.
59 public function __construct($type) {
62 $this->dbprefix
= $this->get_unique_db_prefix($type);
63 // Save a reference to the global $DB so it will not be released while we still have open locks.
66 \core_shutdown_manager
::register_function([$this, 'auto_release']);
71 * @return boolean - True if this lock type is available in this environment.
73 public function is_available() {
74 return $this->db
->get_dbfamily() === 'mysql';
78 * Return information about the blocking behaviour of the lock type on this platform.
79 * @return boolean - Defer to the DB driver.
81 public function supports_timeout() {
86 * Will this lock type will be automatically released when a process ends.
88 * @return boolean - Via shutdown handler.
90 public function supports_auto_release() {
95 * @deprecated since Moodle 3.10.
97 public function supports_recursion() {
98 throw new coding_exception('The function supports_recursion() has been removed, please do not use it anymore.');
102 * Create and get a lock
103 * @param string $resource - The identifier for the lock. Should use frankenstyle prefix.
104 * @param int $timeout - The number of seconds to wait for a lock before giving up.
105 * @param int $maxlifetime - Unused by this lock type.
106 * @return boolean - true if a lock was obtained.
108 public function get_lock($resource, $timeout, $maxlifetime = 86400) {
110 // We sha1 to avoid long key names hitting the mysql str limit.
111 $resourcekey = sha1($this->dbprefix
. $resource);
113 // Even though some versions of MySQL and MariaDB can support stacked locks
114 // just never stack them and always fail fast.
115 if (isset($this->openlocks
[$resourcekey])) {
120 'resourcekey' => $resourcekey,
121 'timeout' => $timeout
124 $result = $this->db
->get_record_sql('SELECT GET_LOCK(:resourcekey, :timeout) AS locked', $params);
125 $locked = $result->locked
== 1;
128 $this->openlocks
[$resourcekey] = 1;
129 return new lock($resourcekey, $this);
135 * Release a lock that was previously obtained with @lock.
136 * @param lock $lock - a lock obtained from this factory.
137 * @return boolean - true if the lock is no longer held (including if it was never held).
139 public function release_lock(lock
$lock) {
142 'resourcekey' => $lock->get_key()
144 $result = $this->db
->get_record_sql('SELECT RELEASE_LOCK(:resourcekey) AS unlocked', $params);
145 $result = $result->unlocked
== 1;
147 unset($this->openlocks
[$lock->get_key()]);
153 * @deprecated since Moodle 3.10.
155 public function extend_lock() {
156 throw new coding_exception('The function extend_lock() has been removed, please do not use it anymore.');
160 * Auto release any open locks on shutdown.
161 * This is required, because we may be using persistent DB connections.
163 public function auto_release() {
164 // Called from the shutdown handler. Must release all open locks.
165 foreach ($this->openlocks
as $key => $unused) {
166 $lock = new lock($key, $this);