Commit 4e1d6670 authored by alexpott's avatar alexpott

Issue #2087931 by damiankloip: Inject database connection into DatabaseLockBackend.

parent 34f638b2
...@@ -221,6 +221,7 @@ services: ...@@ -221,6 +221,7 @@ services:
arguments: ['@container.namespaces', '@cache.cache', '@language_manager', '@module_handler'] arguments: ['@container.namespaces', '@cache.cache', '@language_manager', '@module_handler']
lock: lock:
class: Drupal\Core\Lock\DatabaseLockBackend class: Drupal\Core\Lock\DatabaseLockBackend
arguments: ['@database']
user.tempstore: user.tempstore:
class: Drupal\user\TempStoreFactory class: Drupal\user\TempStoreFactory
arguments: ['@database', '@lock'] arguments: ['@database', '@lock']
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
namespace Drupal\Core\Lock; namespace Drupal\Core\Lock;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\IntegrityConstraintViolationException; use Drupal\Core\Database\IntegrityConstraintViolationException;
/** /**
...@@ -14,13 +15,24 @@ ...@@ -14,13 +15,24 @@
*/ */
class DatabaseLockBackend extends LockBackendAbstract { class DatabaseLockBackend extends LockBackendAbstract {
/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/** /**
* Constructs a new DatabaseLockBackend. * Constructs a new DatabaseLockBackend.
*
* @param \Drupal\Core\Database\Connection $database
* The database connection.
*/ */
public function __construct() { public function __construct(Connection $database) {
// __destruct() is causing problems with garbage collections, register a // __destruct() is causing problems with garbage collections, register a
// shutdown function instead. // shutdown function instead.
drupal_register_shutdown_function(array($this, 'releaseAll')); drupal_register_shutdown_function(array($this, 'releaseAll'));
$this->database = $database;
} }
/** /**
...@@ -32,7 +44,7 @@ public function acquire($name, $timeout = 30.0) { ...@@ -32,7 +44,7 @@ public function acquire($name, $timeout = 30.0) {
$expire = microtime(TRUE) + $timeout; $expire = microtime(TRUE) + $timeout;
if (isset($this->locks[$name])) { if (isset($this->locks[$name])) {
// Try to extend the expiration of a lock we already acquired. // Try to extend the expiration of a lock we already acquired.
$success = (bool) db_update('semaphore') $success = (bool) $this->database->update('semaphore')
->fields(array('expire' => $expire)) ->fields(array('expire' => $expire))
->condition('name', $name) ->condition('name', $name)
->condition('value', $this->getLockId()) ->condition('value', $this->getLockId())
...@@ -50,7 +62,7 @@ public function acquire($name, $timeout = 30.0) { ...@@ -50,7 +62,7 @@ public function acquire($name, $timeout = 30.0) {
// We always want to do this code at least once. // We always want to do this code at least once.
do { do {
try { try {
db_insert('semaphore') $this->database->insert('semaphore')
->fields(array( ->fields(array(
'name' => $name, 'name' => $name,
'value' => $this->getLockId(), 'value' => $this->getLockId(),
...@@ -81,7 +93,7 @@ public function acquire($name, $timeout = 30.0) { ...@@ -81,7 +93,7 @@ public function acquire($name, $timeout = 30.0) {
* Implements Drupal\Core\Lock\LockBackedInterface::lockMayBeAvailable(). * Implements Drupal\Core\Lock\LockBackedInterface::lockMayBeAvailable().
*/ */
public function lockMayBeAvailable($name) { public function lockMayBeAvailable($name) {
$lock = db_query('SELECT expire, value FROM {semaphore} WHERE name = :name', array(':name' => $name))->fetchAssoc(); $lock = $this->database->query('SELECT expire, value FROM {semaphore} WHERE name = :name', array(':name' => $name))->fetchAssoc();
if (!$lock) { if (!$lock) {
return TRUE; return TRUE;
} }
...@@ -91,7 +103,7 @@ public function lockMayBeAvailable($name) { ...@@ -91,7 +103,7 @@ public function lockMayBeAvailable($name) {
// We check two conditions to prevent a race condition where another // 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 // 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. // number to $expire to avoid errors with float to string conversion.
return (bool) db_delete('semaphore') return (bool) $this->database->delete('semaphore')
->condition('name', $name) ->condition('name', $name)
->condition('value', $lock['value']) ->condition('value', $lock['value'])
->condition('expire', 0.0001 + $expire, '<=') ->condition('expire', 0.0001 + $expire, '<=')
...@@ -105,7 +117,7 @@ public function lockMayBeAvailable($name) { ...@@ -105,7 +117,7 @@ public function lockMayBeAvailable($name) {
*/ */
public function release($name) { public function release($name) {
unset($this->locks[$name]); unset($this->locks[$name]);
db_delete('semaphore') $this->database->delete('semaphore')
->condition('name', $name) ->condition('name', $name)
->condition('value', $this->getLockId()) ->condition('value', $this->getLockId())
->execute(); ->execute();
...@@ -121,7 +133,7 @@ public function releaseAll($lock_id = NULL) { ...@@ -121,7 +133,7 @@ public function releaseAll($lock_id = NULL) {
if (empty($lock_id)) { if (empty($lock_id)) {
$lock_id = $this->getLockId(); $lock_id = $this->getLockId();
} }
db_delete('semaphore') $this->database->delete('semaphore')
->condition('value', $lock_id) ->condition('value', $lock_id)
->execute(); ->execute();
} }
......
...@@ -39,7 +39,7 @@ public static function getInfo() { ...@@ -39,7 +39,7 @@ public static function getInfo() {
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
$this->lock = new DatabaseLockBackend(); $this->lock = new DatabaseLockBackend($this->container->get('database'));
$this->installSchema('system', 'semaphore'); $this->installSchema('system', 'semaphore');
} }
......
...@@ -83,7 +83,7 @@ protected function tearDown() { ...@@ -83,7 +83,7 @@ protected function tearDown() {
*/ */
public function testUserTempStore() { public function testUserTempStore() {
// Create a key/value collection. // Create a key/value collection.
$factory = new TempStoreFactory(Database::getConnection(), new DatabaseLockBackend()); $factory = new TempStoreFactory(Database::getConnection(), new DatabaseLockBackend(Database::getConnection()));
$collection = $this->randomName(); $collection = $this->randomName();
// Create two mock users. // Create two mock users.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment