You are here

class DatabaseLockBackend in Drupal driver for SQL Server and SQL Azure 8.2

Same name in this branch
  1. 8.2 src/Lock/DatabaseLockBackend.php \Drupal\sqlsrv\Lock\DatabaseLockBackend
  2. 8.2 src/ProxyClass/Lock/DatabaseLockBackend.php \Drupal\sqlsrv\ProxyClass\Lock\DatabaseLockBackend

Workaround for an issue with floats:

Hierarchy

Expanded class hierarchy of DatabaseLockBackend

See also

https://github.com/Azure/msphpsql/issues/71

File

src/Lock/DatabaseLockBackend.php, line 21
Contains \Drupal\sqlsrv\Lock\DatabaseLockBackend.

Namespace

Drupal\sqlsrv\Lock
View source
class DatabaseLockBackend extends CoreDatabaseLockBackend {

  /**
   * {@inheritdoc}
   */
  public function lockMayBeAvailable($name) {
    $lock = $this->database
      ->query('SELECT CONVERT(varchar(50), expire, 128) as 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;
  }

}

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::catchException protected function Act on an exception when semaphore might be stale.
DatabaseLockBackend::ensureTableExists protected function Check if the semaphore table exists and create it if not.
DatabaseLockBackend::lockMayBeAvailable public function Checks if a lock is available for acquiring. Overrides DatabaseLockBackend::lockMayBeAvailable
DatabaseLockBackend::normalizeName protected function Normalizes a lock name in order to comply with database limitations.
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::schemaDefinition public function Defines the schema for the semaphore table.
DatabaseLockBackend::TABLE_NAME constant The database table name.
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.
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