class DatabaseLock extends Lock implements RefreshableLock

Traits

Properties

protected string $name

The name of the lock.

from  Lock
protected int $seconds

The number of seconds the lock should be maintained.

from  Lock
protected string $owner

The scope identifier of this lock.

from  Lock
protected int $sleepMilliseconds

The number of milliseconds to wait before re-attempting to acquire a lock while blocking.

from  Lock
protected ConnectionResolverInterface $resolver

The database connection resolver.

protected string|null $connectionName

The connection name.

protected string $table

The database table name.

protected array $lottery

The prune probability odds.

protected int $defaultTimeoutInSeconds

The default number of seconds that a lock should be held.

Methods

int
secondsUntil(DateInterval|DateTimeInterface|int $delay)

Get the number of seconds until the given DateTime.

int
availableAt(DateInterval|DateTimeInterface|int|null $delay = 0)

Get the "available at" UNIX timestamp.

parseDateInterval(DateInterval|DateTimeInterface|int|null $delay)

If the given value is an interval, convert it to a DateTime instance.

int
currentTime()

Get the current system time as a UNIX timestamp.

string
runTimeForHumans(float $startTime, float|null $endTime = null)

Given a start time, format the total run time for human readability.

__construct(ConnectionResolverInterface $resolver, string|null $connectionName, string $name, string $table, int $seconds, string|null $owner = null, array $lottery = [2, 100], int $defaultTimeoutInSeconds = 86400)

Create a new lock instance.

bool
acquire()

Attempt to acquire the lock.

bool
release()

Release the lock.

string|null
getCurrentOwner()

Return the owner value written into the driver for this lock.

mixed
get(callable|null $callback = null)

Attempt to acquire the lock.

from  Lock
mixed
block(int $seconds, callable|null $callback = null)

Attempt to acquire the lock for the given number of seconds.

from  Lock
bool
refresh(int|null $seconds = null)

Refresh the lock's TTL if still owned by this process.

float|null
getRemainingLifetime()

Get the number of seconds until the lock expires.

string
owner()

Return the current owner of the lock.

from  Lock
bool
isLocked()

Determine if the lock is currently held by any process.

from  Lock
bool
isOwnedByCurrentProcess()

Determine whether this lock is allowed to release the lock in the driver.

from  Lock
bool
isOwnedBy(string|null $owner)

Determine whether this lock is owned by the given identifier.

from  Lock
betweenBlockedAttemptsSleepFor(int $milliseconds)

Specify the number of milliseconds to sleep in between blocked lock acquisition attempts.

from  Lock
bool
causedByConcurrencyError(Throwable $e)

Determine if the given exception was caused by a concurrency error such as a deadlock or serialization failure.

connection()

Get a fresh connection from the pool.

void
forceRelease()

Releases this lock in disregard of ownership.

void
pruneExpiredLocks()

Delete locks that are past expiration.

int
expiresAt(int|null $seconds = null)

Get the UNIX timestamp indicating when the lock should expire.

string|null
getConnectionName()

Get the name of the database connection being used to manage the lock.

Details

in InteractsWithTime at line 17
protected int secondsUntil(DateInterval|DateTimeInterface|int $delay)

Get the number of seconds until the given DateTime.

Parameters

DateInterval|DateTimeInterface|int $delay

Return Value

int

in InteractsWithTime at line 29
protected int availableAt(DateInterval|DateTimeInterface|int|null $delay = 0)

Get the "available at" UNIX timestamp.

Parameters

DateInterval|DateTimeInterface|int|null $delay

Return Value

int

in InteractsWithTime at line 41
protected DateTimeInterface|int parseDateInterval(DateInterval|DateTimeInterface|int|null $delay)

If the given value is an interval, convert it to a DateTime instance.

Parameters

DateInterval|DateTimeInterface|int|null $delay

Return Value

DateTimeInterface|int

in InteractsWithTime at line 57
protected int currentTime()

Get the current system time as a UNIX timestamp.

Return Value

int

in InteractsWithTime at line 65
protected string runTimeForHumans(float $startTime, float|null $endTime = null)

Given a start time, format the total run time for human readability.

Parameters

float $startTime
float|null $endTime

Return Value

string

at line 47
__construct(ConnectionResolverInterface $resolver, string|null $connectionName, string $name, string $table, int $seconds, string|null $owner = null, array $lottery = [2, 100], int $defaultTimeoutInSeconds = 86400)

Create a new lock instance.

Parameters

ConnectionResolverInterface $resolver
string|null $connectionName
string $name
string $table
int $seconds
string|null $owner
array $lottery
int $defaultTimeoutInSeconds

at line 77
bool acquire()

Attempt to acquire the lock.

Return Value

bool

at line 114
bool release()

Release the lock.

Return Value

bool

Exceptions

Throwable

at line 167
protected string|null getCurrentOwner()

Return the owner value written into the driver for this lock.

Return Value

string|null

in Lock at line 73
mixed get(callable|null $callback = null)

Attempt to acquire the lock.

Parameters

callable|null $callback

Return Value

mixed

in Lock at line 101
mixed block(int $seconds, callable|null $callback = null)

Attempt to acquire the lock for the given number of seconds.

Parameters

int $seconds
callable|null $callback

Return Value

mixed

Exceptions

LockTimeoutException

at line 197
bool refresh(int|null $seconds = null)

Refresh the lock's TTL if still owned by this process.

Database locks are never truly permanent: non-positive acquisition durations use the default timeout so a crashed process cannot leave an unreclaimable row. refresh(null) reapplies that acquisition rule.

Parameters

int|null $seconds

Return Value

bool

Exceptions

InvalidArgumentException

at line 217
float|null getRemainingLifetime()

Get the number of seconds until the lock expires.

Return Value

float|null

in Lock at line 161
string owner()

Return the current owner of the lock.

Return Value

string

in Lock at line 169
bool isLocked()

Determine if the lock is currently held by any process.

Return Value

bool

in Lock at line 177
bool isOwnedByCurrentProcess()

Determine whether this lock is allowed to release the lock in the driver.

Return Value

bool

in Lock at line 185
bool isOwnedBy(string|null $owner)

Determine whether this lock is owned by the given identifier.

Parameters

string|null $owner

Return Value

bool

in Lock at line 193
Lock betweenBlockedAttemptsSleepFor(int $milliseconds)

Specify the number of milliseconds to sleep in between blocked lock acquisition attempts.

Parameters

int $milliseconds

Return Value

Lock

protected bool causedByConcurrencyError(Throwable $e)

Determine if the given exception was caused by a concurrency error such as a deadlock or serialization failure.

Parameters

Throwable $e

Return Value

bool

at line 69
protected ConnectionInterface connection()

Get a fresh connection from the pool.

Return Value

ConnectionInterface

at line 139
void forceRelease()

Releases this lock in disregard of ownership.

Return Value

void

at line 151
void pruneExpiredLocks()

Delete locks that are past expiration.

Return Value

void

Exceptions

Throwable

at line 179
protected int expiresAt(int|null $seconds = null)

Get the UNIX timestamp indicating when the lock should expire.

Parameters

int|null $seconds

Return Value

int

at line 236
string|null getConnectionName()

Get the name of the database connection being used to manage the lock.

Return Value

string|null