DatabaseLock
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 null|array{int, int}|array{} | $lottery | The prune probability odds. |
|
| protected int | $defaultTimeoutInSeconds | The default number of seconds that a lock should be held. |
Methods
Get the number of seconds until the given DateTime.
Get the "available at" UNIX timestamp.
If the given value is an interval, convert it to a DateTime instance.
Given a start time, format the total run time for human readability.
Create a new lock instance.
Attempt to acquire the lock.
Release the lock.
Return the owner value written into the driver for this lock.
Attempt to acquire the lock for the given number of seconds.
Refresh the lock's TTL if still owned by this process.
Get the number of seconds until the lock expires.
Determine whether this lock is allowed to release the lock in the driver.
Determine whether this lock is owned by the given identifier.
Specify the number of milliseconds to sleep in between blocked lock acquisition attempts.
Determine if the given exception was caused by a concurrency error such as a deadlock or serialization failure.
Get a fresh connection from the pool.
Releases this lock in disregard of ownership.
Delete locks that are past expiration.
Determine whether this owner holds the live lock after an update that changed no row.
Get the UNIX timestamp indicating when the lock should expire.
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.
in
InteractsWithTime at line 29
protected int
availableAt(DateInterval|DateTimeInterface|int|null $delay = 0)
Get the "available at" UNIX timestamp.
in
InteractsWithTime at line 47
protected DateTimeInterface|int
parseDateInterval(DateInterval|DateTimeInterface|int|null $delay)
If the given value is an interval, convert it to a DateTime instance.
in
InteractsWithTime at line 63
protected int
currentTime()
Get the current system time as a UNIX timestamp.
in
InteractsWithTime at line 71
protected string
runTimeForHumans(float $startTime, float|null $endTime = null)
Given a start time, format the total run time for human readability.
at line 51
__construct(ConnectionResolverInterface $resolver, string|null $connectionName, string $name, string $table, int $seconds, string|null $owner = null, array|null $lottery = [2, 100], int $defaultTimeoutInSeconds = 86400)
Create a new lock instance.
at line 83
bool
acquire()
Attempt to acquire the lock.
at line 143
bool
release()
Release the lock.
at line 196
protected string|null
getCurrentOwner()
Return the owner value written into the driver for this lock.
in
Lock at line 74
mixed
get(callable|null $callback = null)
Attempt to acquire the lock.
in
Lock at line 96
mixed
block(int $seconds, callable|null $callback = null)
Attempt to acquire the lock for the given number of seconds.
in
Lock at line 130
protected never
releaseAfterFailure(Throwable $failure)
Release the lock while preserving the primary failure.
at line 251
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.
at line 273
float|null
getRemainingLifetime()
Get the number of seconds until the lock expires.
in
Lock at line 164
string
owner()
Return the current owner of the lock.
in
Lock at line 172
bool
isLocked()
Determine if the lock is currently held by any process.
in
Lock at line 180
bool
isOwnedByCurrentProcess()
Determine whether this lock is allowed to release the lock in the driver.
in
Lock at line 188
bool
isOwnedBy(string|null $owner)
Determine whether this lock is owned by the given identifier.
in
Lock at line 198
Lock
betweenBlockedAttemptsSleepFor(int $milliseconds)
Specify the number of milliseconds to sleep in between blocked lock acquisition attempts.
in
DetectsConcurrencyErrors at line 16
protected bool
causedByConcurrencyError(Throwable $e)
Determine if the given exception was caused by a concurrency error such as a deadlock or serialization failure.
at line 73
protected ConnectionInterface
connection()
Get a fresh connection from the pool.
at line 165
void
forceRelease()
Releases this lock in disregard of ownership.
at line 177
void
pruneExpiredLocks()
Delete locks that are past expiration.
at line 214
protected bool
ownsLockAfterUnchangedUpdate(ConnectionInterface $connection)
Determine whether this owner holds the live lock after an update that changed no row.
MySQL and MariaDB count only changed rows, so an owner writing the expiration its lock already has, such as within the second it was acquired, updates none. Other drivers count matched rows, so their zero means the lock is not this owner's.
at line 233
protected int
expiresAt(int|null $seconds = null)
Get the UNIX timestamp indicating when the lock should expire.
at line 293
string|null
getConnectionName()
Get the name of the database connection being used to manage the lock.