PHP code example of yiisoft / mutex-pdo-oracle

1. Go to this page and download the library: Download yiisoft/mutex-pdo-oracle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yiisoft / mutex-pdo-oracle example snippets


use Yiisoft\Mutex\Oracle\OracleMutex;
use Yiisoft\Mutex\Oracle\OracleMutexFactory;

/**
 * @var \PDO $connection Configured for Oracle.
 */

$mutex = new OracleMutex(
    'mutex-name',
    $connection,
    OracleMutex::MODE_X, // Optional. Lock mode to be used. Default is `OracleMutex::MODE_X`.
    false, // Optional. Whether to release lock on commit. Default is `false`.
);

$mutexFactory = new OracleMutexFactory(
    $connection,
    OracleMutex::MODE_X, // Optional. Lock mode to be used. Default is `OracleMutex::MODE_X`.
    false, // Optional. Whether to release lock on commit. Default is `false`.
);

$synchronizer = new \Yiisoft\Mutex\Synchronizer($mutexFactory);

$newCount = $synchronizer->execute('critical', function () {
    return $counter->increase();
}, 10);

$simpleMutex = \Yiisoft\Mutex\SimpleMutex($mutexFactory);

if (!$simpleMutex->acquire('critical', 10)) {
    throw new \Yiisoft\Mutex\Exception\MutexLockedException('Unable to acquire the "critical" mutex.');
}

$newCount = $counter->increase();
$simpleMutex->release('critical');

$mutex = $mutexFactory->createAndAcquire('critical', 10);
$newCount = $counter->increase();
$mutex->release();

$mutex = $mutexFactory->create('critical');

if (!$mutex->acquire(10)) {
    throw new \Yiisoft\Mutex\Exception\MutexLockedException('Unable to acquire the "critical" mutex.');
}

$newCount = $counter->increase();
$mutex->release();

$mutex = $mutex->withRetryDelay(100);