You are here

class DatabaseLockBackend in Zircon Profile 8

Same name in this branch
  1. 8 core/lib/Drupal/Core/Lock/DatabaseLockBackend.php \Drupal\Core\Lock\DatabaseLockBackend
  2. 8 core/lib/Drupal/Core/ProxyClass/Lock/DatabaseLockBackend.php \Drupal\Core\ProxyClass\Lock\DatabaseLockBackend
Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Lock/DatabaseLockBackend.php \Drupal\Core\Lock\DatabaseLockBackend

Defines the database lock backend. This is the default backend in Drupal.

Hierarchy

Expanded class hierarchy of DatabaseLockBackend

Related topics

2 files declare their use of DatabaseLockBackend
LockUnitTest.php in core/modules/system/src/Tests/Lock/LockUnitTest.php
Contains \Drupal\system\Tests\Lock\LockUnitTest.
TempStoreDatabaseTest.php in core/modules/user/src/Tests/TempStoreDatabaseTest.php
Contains \Drupal\user\Tests\TempStoreDatabaseTest.
1 string reference to 'DatabaseLockBackend'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses DatabaseLockBackend
lock in core/core.services.yml
Drupal\Core\Lock\DatabaseLockBackend

File

core/lib/Drupal/Core/Lock/DatabaseLockBackend.php, line 18
Contains \Drupal\Core\Lock\DatabaseLockBackend.

Namespace

Drupal\Core\Lock
View source
class DatabaseLockBackend extends LockBackendAbstract {

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * Constructs a new DatabaseLockBackend.
   *
   * @param \Drupal\Core\Database\Connection $database
   *   The database connection.
   */
  public function __construct(Connection $database) {

    // __destruct() is causing problems with garbage collections, register a
    // shutdown function instead.
    drupal_register_shutdown_function(array(
      $this,
      'releaseAll',
    ));
    $this->database = $database;
  }

  /**
   * {@inheritdoc}
   */
  public function acquire($name, $timeout = 30.0) {

    // Insure that the timeout is at least 1 ms.
    $timeout = max($timeout, 0.001);
    $expire = microtime(TRUE) + $timeout;
    if (isset($this->locks[$name])) {

      // Try to extend the expiration of a lock we already acquired.
      $success = (bool) $this->database
        ->update('semaphore')
        ->fields(array(
        'expire' => $expire,
      ))
        ->condition('name', $name)
        ->condition('value', $this
        ->getLockId())
        ->execute();
      if (!$success) {

        // The lock was broken.
        unset($this->locks[$name]);
      }
      return $success;
    }
    else {

      // Optimistically try to acquire the lock, then retry once if it fails.
      // The first time through the loop cannot be a retry.
      $retry = FALSE;

      // We always want to do this code at least once.
      do {
        try {
          $this->database
            ->insert('semaphore')
            ->fields(array(
            'name' => $name,
            'value' => $this
              ->getLockId(),
            'expire' => $expire,
          ))
            ->execute();

          // We track all acquired locks in the global variable.
          $this->locks[$name] = TRUE;

          // We never need to try again.
          $retry = FALSE;
        } catch (IntegrityConstraintViolationException $e) {

          // Suppress the error. If this is our first pass through the loop,
          // then $retry is FALSE. In this case, the insert failed because some
          // other request acquired the lock but did not release it. We decide
          // whether to retry by checking lockMayBeAvailable(). This will clear
          // the offending row from the database table in case it has expired.
          $retry = $retry ? FALSE : $this
            ->lockMayBeAvailable($name);
        }

        // We only retry in case the first attempt failed, but we then broke
        // an expired lock.
      } while ($retry);
    }
    return isset($this->locks[$name]);
  }

  /**
   * {@inheritdoc}
   */
  public function lockMayBeAvailable($name) {
    $lock = $this->database
      ->query('SELECT expire, value FROM {semaphore} WHERE name = :name', array(
      ':name' => $name,
    ))
      ->fetchAssoc();
    if (!$lock) {
      return TRUE;
    }
    $expire = (double) $lock['expire'];
    $now = microtime(TRUE);
    if ($now > $expire) {

      // We check two conditions to prevent a race condition where another
      // request acquired the lock and set a new expire time. We add a small
      // number to $expire to avoid errors with float to string conversion.
      return (bool) $this->database
        ->delete('semaphore')
        ->condition('name', $name)
        ->condition('value', $lock['value'])
        ->condition('expire', 0.0001 + $expire, '<=')
        ->execute();
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function release($name) {
    unset($this->locks[$name]);
    $this->database
      ->delete('semaphore')
      ->condition('name', $name)
      ->condition('value', $this
      ->getLockId())
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function releaseAll($lock_id = NULL) {

    // Only attempt to release locks if any were acquired.
    if (!empty($this->locks)) {
      $this->locks = array();
      if (empty($lock_id)) {
        $lock_id = $this
          ->getLockId();
      }
      $this->database
        ->delete('semaphore')
        ->condition('value', $lock_id)
        ->execute();
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DatabaseLockBackend::$database protected property The database connection.
DatabaseLockBackend::acquire public function Acquires a lock. Overrides LockBackendInterface::acquire
DatabaseLockBackend::lockMayBeAvailable public function Checks if a lock is available for acquiring. Overrides LockBackendInterface::lockMayBeAvailable
DatabaseLockBackend::release public function Releases the given lock. Overrides LockBackendInterface::release
DatabaseLockBackend::releaseAll public function Releases all locks for the given lock token identifier. Overrides LockBackendInterface::releaseAll
DatabaseLockBackend::__construct public function Constructs a new DatabaseLockBackend. 1
LockBackendAbstract::$lockId protected property Current page lock token identifier.
LockBackendAbstract::$locks protected property Existing locks for this page. 1
LockBackendAbstract::getLockId public function Gets the unique page token for locks. Overrides LockBackendInterface::getLockId
LockBackendAbstract::wait public function Waits a short amount of time before a second lock acquire attempt. Overrides LockBackendInterface::wait