class DatabaseLockBackend extends LockBackendAbstract (View source)

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

Constants

TABLE_NAME

The database table name.

Properties

protected string $lockId

Current page lock token identifier.

from  LockBackendAbstract
protected array $locks

Existing locks for this page.

from  LockBackendAbstract
protected Connection $database

The database connection.

Methods

bool
wait(string $name, int $delay = 30)

Waits a short amount of time before a second lock acquire attempt.

string
getLockId()

Gets the unique page token for locks.

__construct(Connection $database)

Constructs a new DatabaseLockBackend.

bool
acquire(string $name, float $timeout = 30.0)

Acquires a lock.

bool
lockMayBeAvailable(string $name)

Checks if a lock is available for acquiring.

release(string $name)

Releases the given lock.

releaseAll($lock_id = NULL)

Releases all locks for the given lock token identifier.

ensureTableExists()

Check if the semaphore table exists and create it if not.

catchException(Exception $e)

Act on an exception when semaphore might be stale.

string
normalizeName(string $name)

Normalizes a lock name in order to comply with database limitations.

schemaDefinition()

Defines the schema for the semaphore table.

Details

bool wait(string $name, int $delay = 30)

Waits a short amount of time before a second lock acquire attempt.

While this method is subject to have a generic implementation in abstract backend implementation, some backends may provide non blocking or less I/O intensive wait mechanism: this is why this method remains on the backend interface.

Parameters

string $name

Lock name currently being locked.

int $delay

Seconds to wait for. Defaults to 30.

Return Value

bool

TRUE if the lock holds, FALSE if it may be available. You still need to acquire the lock manually and it may fail again.

string getLockId()

Gets the unique page token for locks.

Locks will be wiped out at the end of each page request on a token basis.

Return Value

string

__construct(Connection $database)

Constructs a new DatabaseLockBackend.

Parameters

Connection $database

The database connection.

bool acquire(string $name, float $timeout = 30.0)

Acquires a lock.

Parameters

string $name

Lock name. Limit of name's length is 255 characters.

float $timeout

(optional) Lock lifetime in seconds. Defaults to 30.0.

Return Value

bool

bool lockMayBeAvailable(string $name)

Checks if a lock is available for acquiring.

Parameters

string $name

Lock to acquire.

Return Value

bool

release(string $name)

Releases the given lock.

Parameters

string $name

The lock name.

releaseAll($lock_id = NULL)

Releases all locks for the given lock token identifier.

Parameters

$lock_id

protected ensureTableExists()

Check if the semaphore table exists and create it if not.

protected catchException(Exception $e)

Act on an exception when semaphore might be stale.

If the table does not yet exist, that's fine, but if the table exists and yet the query failed, then the semaphore is stale and the exception needs to propagate.

Parameters

Exception $e

The exception.

Exceptions

Exception

protected string normalizeName(string $name)

Normalizes a lock name in order to comply with database limitations.

Parameters

string $name

The passed in lock name.

Return Value

string

An ASCII-encoded lock name that is at most 255 characters long.

schemaDefinition()

internal  
 

Defines the schema for the semaphore table.