Transaction
in package
Description of Transaction
Tags
Table of Contents
Constants
- LOCK_TIMEOUT_DEFAULT = 10000
- PG_DEADLOCK = '40P01'
- PG_DUPLICATE_KEY = '23505'
- PG_FOREIGN_KEY_VIOLATION = '23503'
- PG_LOCK_FAILURE = '55P03'
- PG_WRONG_BINARY_VALUE = '22P03'
- PG_WRONG_DATE_VALUE = '22007'
- PG_WRONG_TEXT_VALUE = '22P02'
- STATE_ACTIVE = 'active'
- STATE_COMMIT = 'commit'
- STATE_LOCKED = 'locked'
- STATE_NOTX = 'notx'
- STATE_ROLLBACK = 'rollback'
- STMT_TIMEOUT_DEFAULT = 60000
Properties
- $id : int|null
- $lastRequest : string
- $lockedResources : bool
- $pdo : PDO
- Database connection.
- $snapshot : string
- $startedAt : string
- $state : string
Methods
- __construct() : mixed
- createResource() : int
- delete() : void
- deleteResource() : void
- get() : void
- getId() : int|null
- getPreTransactionDbHandle() : PDO
- getState() : string|null
- head() : void
- lockResource() : string|null
- Locks a given resource with a given lock id and the current transaction id.
- options() : void
- post() : void
- prolong() : void
- put() : void
- unlockResources() : void
- executeQuery() : void
- Executes a given PDO statement trapping the lock timeout and foreign key database exceptions and turning them it into the `ConflictException`.
- fetchData() : void
- getResourceList() : array<string|int, int>
- lock() : void
- Tries to lock the current transaction.
- releaseAndWait() : void
- Actively waits until the transaction controller daemon rollbacks/commits the transaction
- setState() : void
Constants
LOCK_TIMEOUT_DEFAULT
public
mixed
LOCK_TIMEOUT_DEFAULT
= 10000
PG_DEADLOCK
public
mixed
PG_DEADLOCK
= '40P01'
PG_DUPLICATE_KEY
public
mixed
PG_DUPLICATE_KEY
= '23505'
PG_FOREIGN_KEY_VIOLATION
public
mixed
PG_FOREIGN_KEY_VIOLATION
= '23503'
PG_LOCK_FAILURE
public
mixed
PG_LOCK_FAILURE
= '55P03'
PG_WRONG_BINARY_VALUE
public
mixed
PG_WRONG_BINARY_VALUE
= '22P03'
PG_WRONG_DATE_VALUE
public
mixed
PG_WRONG_DATE_VALUE
= '22007'
PG_WRONG_TEXT_VALUE
public
mixed
PG_WRONG_TEXT_VALUE
= '22P02'
STATE_ACTIVE
public
mixed
STATE_ACTIVE
= 'active'
STATE_COMMIT
public
mixed
STATE_COMMIT
= 'commit'
STATE_LOCKED
public
mixed
STATE_LOCKED
= 'locked'
STATE_NOTX
public
mixed
STATE_NOTX
= 'notx'
STATE_ROLLBACK
public
mixed
STATE_ROLLBACK
= 'rollback'
STMT_TIMEOUT_DEFAULT
public
mixed
STMT_TIMEOUT_DEFAULT
= 60000
Properties
$id
private
int|null
$id
= null
$lastRequest
private
string
$lastRequest
= ''
$lockedResources
private
bool
$lockedResources
= false
$pdo
Database connection.
private
PDO
$pdo
A separate is required so it can commit changes independently from the main connection.
$snapshot
private
string
$snapshot
$startedAt
private
string
$startedAt
= ''
$state
private
string
$state
= self::STATE_NOTX
Methods
__construct()
public
__construct() : mixed
createResource()
public
createResource(int $lockId[, array<string|int, string> $ids = [] ]) : int
Parameters
- $lockId : int
- $ids : array<string|int, string> = []
Tags
Return values
intdelete()
public
delete() : void
deleteResource()
public
deleteResource(int $resId) : void
Parameters
- $resId : int
get()
public
get() : void
getId()
public
getId() : int|null
Return values
int|nullgetPreTransactionDbHandle()
public
getPreTransactionDbHandle() : PDO
Return values
PDOgetState()
public
getState() : string|null
Return values
string|nullhead()
public
head([bool $get = false ]) : void
Parameters
- $get : bool = false
lockResource()
Locks a given resource with a given lock id and the current transaction id.
public
lockResource(int $resId, int $lockId) : string|null
- First a database lock on the transaction is obtained with
$this->lock(false)
. - Then a database lock on the resource is obtained and the resource is
marked as arche-locked by setting up
transaction_id
andlock
column values - Finally the database transaction is commited.
Parameters
- $resId : int
- $lockId : int
Tags
Return values
string|nulloptions()
public
options([int $code = 204 ]) : void
Parameters
- $code : int = 204
post()
public
post() : void
prolong()
public
prolong() : void
put()
public
put() : void
unlockResources()
public
unlockResources(int $lockId) : void
Parameters
- $lockId : int
executeQuery()
Executes a given PDO statement trapping the lock timeout and foreign key database exceptions and turning them it into the `ConflictException`.
private
executeQuery(PDOStatement $query, array<string|int, mixed> $param[, string $errorMsg = "Database lock timeout" ][, bool $logException = false ]) : void
Parameters
- $query : PDOStatement
- $param : array<string|int, mixed>
- $errorMsg : string = "Database lock timeout"
- $logException : bool = false
Tags
fetchData()
private
fetchData() : void
getResourceList()
private
getResourceList() : array<string|int, int>
Return values
array<string|int, int>lock()
Tries to lock the current transaction.
private
lock([bool $checkResourceLocks = true ]) : void
Assures no other transaction PUT(commit)/DELETE(rollback) is executed in parallel. Will succeed only if there's no this transaction related resource operation being executed.
Lock persists until $this->pdo->commit()/rollBack();
Parameters
- $checkResourceLocks : bool = true
Tags
releaseAndWait()
Actively waits until the transaction controller daemon rollbacks/commits the transaction
private
releaseAndWait() : void
setState()
private
setState(string $state) : void
Parameters
- $state : string