0 follower

Final Class Yiisoft\Mutex\Oracle\OracleMutex

InheritanceYiisoft\Mutex\Oracle\OracleMutex » Yiisoft\Mutex\Mutex

OracleMutex implements mutex "lock" mechanism via Oracle locks.

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Mutex\Oracle\OracleMutex

Constants

Hide inherited constants

Constant Value Description Defined By
MODES [ self::MODE_X, self::MODE_NL, self::MODE_S, self::MODE_SX, self::MODE_SS, self::MODE_SSX, ] Yiisoft\Mutex\Oracle\OracleMutex
MODE_NL 'NL_MODE' Yiisoft\Mutex\Oracle\OracleMutex
MODE_S 'S_MODE' Yiisoft\Mutex\Oracle\OracleMutex
MODE_SS 'SS_MODE' Yiisoft\Mutex\Oracle\OracleMutex
MODE_SSX 'SSX_MODE' Yiisoft\Mutex\Oracle\OracleMutex
MODE_SX 'SX_MODE' Yiisoft\Mutex\Oracle\OracleMutex
MODE_X 'X_MODE' Available lock modes Yiisoft\Mutex\Oracle\OracleMutex

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( string $name, PDO $connection, string $lockMode self::MODE_X, boolean $releaseOnCommit false )
$name string

Mutex name.

$connection PDO

PDO connection instance to use.

$lockMode string

Lock mode to be used {@see https://docs.oracle.com/en/database/oracle/oracle-database/21/arpls/DBMS_LOCK.html#GUID-8F868C41-CEA3-48E2-8701-3C0F8D2B308C}.

$releaseOnCommit boolean

Whether to release lock on commit.

                public function __construct(
    string $name,
    PDO $connection,
    string $lockMode = self::MODE_X,
    bool $releaseOnCommit = false
) {
    if (!in_array($lockMode, self::MODES, true)) {
        throw new InvalidArgumentException(sprintf(
            '"%s" is not valid lock mode for "%s". It must be one of the values of: "%s".',
            $lockMode,
            self::class,
            implode('", "', self::MODES),
        ));
    }
    $this->lockName = $name;
    $this->connection = $connection;
    $this->lockMode = $lockMode;
    $this->releaseOnCommit = $releaseOnCommit;
    /** @var string $driverName */
    $driverName = $connection->getAttribute(PDO::ATTR_DRIVER_NAME);
    if (!in_array($driverName, ['oci', 'obdb'], true)) {
        throw new InvalidArgumentException("Oracle connection instance should be passed. Got \"$driverName\".");
    }
    parent::__construct(self::class, $name);
}

            
acquireLock() protected method
protected acquireLock ( integer $timeout 0 )
$timeout integer

                protected function acquireLock(int $timeout = 0): bool
{
    $lockStatus = 4;
    // clean vars before using
    $releaseOnCommit = $this->releaseOnCommit ? 'TRUE' : 'FALSE';
    // inside pl/sql scopes pdo binding not working correctly :(
    $statement = $this->connection->prepare(
        "DECLARE
            handle VARCHAR2(128);
        BEGIN
            DBMS_LOCK.ALLOCATE_UNIQUE(:name, handle);
            :lockStatus := DBMS_LOCK.REQUEST(
                handle,
                DBMS_LOCK.$this->lockMode,
                $timeout,
                $releaseOnCommit
            );
        END;"
    );
    $statement->bindValue(':name', $this->lockName);
    $statement->bindParam(':lockStatus', $lockStatus, PDO::PARAM_INT, 1);
    $statement->execute();
    return $lockStatus === 0 || $lockStatus === '0';
}

            
releaseLock() protected method
protected releaseLock ( )

                protected function releaseLock(): bool
{
    $releaseStatus = 4;
    $statement = $this->connection->prepare(
        'DECLARE
            handle VARCHAR2(128);
        BEGIN
            DBMS_LOCK.ALLOCATE_UNIQUE(:name, handle);
            :releaseStatus := DBMS_LOCK.RELEASE(handle);
        END;'
    );
    $statement->bindValue(':name', $this->lockName);
    $statement->bindParam(':releaseStatus', $releaseStatus, PDO::PARAM_INT, 1);
    $statement->execute();
    return $releaseStatus === 0 || $releaseStatus === '0';
}