Commit 4e1d6670 authored by alexpott's avatar alexpott

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

parent 34f638b2
......@@ -221,6 +221,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']
......
......@@ -7,6 +7,7 @@
namespace Drupal\Core\Lock;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\IntegrityConstraintViolationException;
/**
......@@ -14,13 +15,24 @@
*/
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() {
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;
}
/**
......@@ -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->database->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->database->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->database->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->database->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->database->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->database->delete('semaphore')
->condition('value', $lock_id)
->execute();
}
......
......@@ -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');
}
......
......@@ -83,7 +83,7 @@ protected function tearDown() {
*/
public function testUserTempStore() {
// Create a key/value collection.
$factory = new TempStoreFactory(Database::getConnection(), new DatabaseLockBackend());
$factory = new TempStoreFactory(Database::getConnection(), new DatabaseLockBackend(Database::getConnection()));
$collection = $this->randomName();
// 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