diff --git a/core/core.services.yml b/core/core.services.yml index 868d4ab..f09561c 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -218,6 +218,7 @@ services: arguments: ['@container.namespaces', '@cache.cache', '@language_manager', '@module_handler'] lock: class: Drupal\Core\Lock\DatabaseLockBackend + arguments: ['@database'] user.tempstore: class: Drupal\user\TempStoreFactory arguments: ['@database', '@lock'] diff --git a/core/lib/Drupal/Core/Lock/DatabaseLockBackend.php b/core/lib/Drupal/Core/Lock/DatabaseLockBackend.php index eef63b4..cd74a98 100644 --- a/core/lib/Drupal/Core/Lock/DatabaseLockBackend.php +++ b/core/lib/Drupal/Core/Lock/DatabaseLockBackend.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Lock; +use Drupal\Core\Database\Connection; use Drupal\Core\Database\IntegrityConstraintViolationException; /** @@ -15,12 +16,23 @@ class DatabaseLockBackend extends LockBackendAbstract { /** + * The database connection. + * + * @var \Drupal\Core\Database\Connection + */ + protected $connection; + + /** * Constructs a new DatabaseLockBackend. + * + * @param \Drupal\Core\Database\Connection $connection + * The database connection. */ - public function __construct() { + public function __construct(Connection $connection) { // __destruct() is causing problems with garbage collections, register a // shutdown function instead. drupal_register_shutdown_function(array($this, 'releaseAll')); + $this->connection = $connection; } /** @@ -32,7 +44,7 @@ public function acquire($name, $timeout = 30.0) { $expire = microtime(TRUE) + $timeout; if (isset($this->locks[$name])) { // Try to extend the expiration of a lock we already acquired. - $success = (bool) db_update('semaphore') + $success = (bool) $this->connection->update('semaphore') ->fields(array('expire' => $expire)) ->condition('name', $name) ->condition('value', $this->getLockId()) @@ -50,7 +62,7 @@ public function acquire($name, $timeout = 30.0) { // We always want to do this code at least once. do { try { - db_insert('semaphore') + $this->connection->insert('semaphore') ->fields(array( 'name' => $name, 'value' => $this->getLockId(), @@ -81,7 +93,7 @@ public function acquire($name, $timeout = 30.0) { * Implements Drupal\Core\Lock\LockBackedInterface::lockMayBeAvailable(). */ public function lockMayBeAvailable($name) { - $lock = db_query('SELECT expire, value FROM {semaphore} WHERE name = :name', array(':name' => $name))->fetchAssoc(); + $lock = $this->connection->query('SELECT expire, value FROM {semaphore} WHERE name = :name', array(':name' => $name))->fetchAssoc(); if (!$lock) { return TRUE; } @@ -91,7 +103,7 @@ public function lockMayBeAvailable($name) { // 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) db_delete('semaphore') + return (bool) $this->connection->delete('semaphore') ->condition('name', $name) ->condition('value', $lock['value']) ->condition('expire', 0.0001 + $expire, '<=') @@ -105,7 +117,7 @@ public function lockMayBeAvailable($name) { */ public function release($name) { unset($this->locks[$name]); - db_delete('semaphore') + $this->connection->delete('semaphore') ->condition('name', $name) ->condition('value', $this->getLockId()) ->execute(); @@ -121,7 +133,7 @@ public function releaseAll($lock_id = NULL) { if (empty($lock_id)) { $lock_id = $this->getLockId(); } - db_delete('semaphore') + $this->connection->delete('semaphore') ->condition('value', $lock_id) ->execute(); } diff --git a/core/modules/system/lib/Drupal/system/Tests/Lock/LockUnitTest.php b/core/modules/system/lib/Drupal/system/Tests/Lock/LockUnitTest.php index b1666f3..db5f908 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Lock/LockUnitTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Lock/LockUnitTest.php @@ -39,7 +39,7 @@ public static function getInfo() { public function setUp() { parent::setUp(); - $this->lock = new DatabaseLockBackend(); + $this->lock = new DatabaseLockBackend($this->container->get('database')); $this->installSchema('system', 'semaphore'); }