Browse Source

Added Oracle mutex

tags/2.0.10
Alexander Zlakomanov 8 years ago committed by Klimov Paul
parent
commit
9d5bf60a89
  1. 1302
      composer.lock
  2. 3
      framework/CHANGELOG.md
  3. 130
      framework/mutex/OracleMutex.php

1302
composer.lock generated

File diff suppressed because it is too large Load Diff

3
framework/CHANGELOG.md

@ -8,6 +8,7 @@ Yii Framework 2 Change Log
- Bug #11461: Fixed migration tool error when create migrate with comma in defaultValue (pana1990, s-o-f)
- Bug #11912: Fixed PostgreSQL Schema to support negative default values for integer/float/decimal columns (nsknewbie)
- Bug #11947: Fixed `gridData` initialization in `yii.gridView.js` (pavlm)
- Enh #11979: Oracle mutex (zlakomanoff)
- Bug #11949: Fixed `ActiveField::end` generates close tag when it's `option['tag']` is null (egorio)
- Enh #11275: Added possibility of unset or force replace former value in `ArrayHelper::merge()` (mdmunir, rob006)
- Enh #11950: Improve BaseArrayHelper::keyExists speed (egorio)
@ -19,6 +20,8 @@ Yii Framework 2 Change Log
- Enh #10583: Do not silence session errors in debug mode (samdark)
- Enh #12048: Improved message extraction command performance (samdark)
- Enh #12038: Introduced `yii\base\ViewNotFoundException` which is thrown when views file doesn't exists, used it in `ViewAction` (samdark)
- Enh #11979: Oracle mutex (zlakomanoff)
2.0.9 July 11, 2016
-------------------

130
framework/mutex/OracleMutex.php

@ -0,0 +1,130 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\mutex;
use PDO;
use Yii;
use yii\base\InvalidConfigException;
/**
* OracleMutex implements mutex "lock" mechanism via Oracle locks.
*
* Application configuration example:
*
* ```
* [
* 'components' => [
* 'db' => [
* 'class' => 'yii\db\Connection',
* 'dsn' => 'oci:dbname=LOCAL_XE',
* ...
* ]
* 'mutex' => [
* 'class' => 'yii\mutex\OracleMutex',
* 'lockMode' => 'NL_MODE',
* 'releaseOnCommit' => true,
* ...
* ],
* ],
* ]
* ```
*
* @see http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/d_lock.htm
* @see Mutex
*
* @author Alexander Zlakomanov <zlakomanoff@gmail.com>
* @since 2.0.10
*/
class OracleMutex extends DbMutex
{
/** available lock modes */
const MODE_X = 'X_MODE';
const MODE_NL = 'NL_MODE';
const MODE_S = 'S_MODE';
const MODE_SX = 'SX_MODE';
const MODE_SS = 'SS_MODE';
const MODE_SSX = 'SSX_MODE';
/**
* @var string lock mode to be used.
* @see http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/d_lock.htm#CHDBCFDI
*/
public $lockMode = self::MODE_X;
/**
* @var boolean whether to release lock on commit.
*/
public $releaseOnCommit = false;
/**
* Initializes Oracle specific mutex component implementation.
* @throws InvalidConfigException if [[db]] is not Oracle connection.
*/
public function init()
{
parent::init();
if (strpos($this->db->driverName, 'oci') !== 0 && strpos($this->db->driverName, 'odbc') !== 0) {
throw new InvalidConfigException('In order to use OracleMutex connection must be configured to use Oracle database.');
}
}
/**
* Acquires lock by given name.
* @see http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/d_lock.htm
* @param string $name of the lock to be acquired.
* @param integer $timeout to wait for lock to become released.
* @return bool acquiring result.
*/
protected function acquireLock($name, $timeout = 0)
{
$lockStatus = null;
/** clean vars before using */
$releaseOnCommit = $this->releaseOnCommit ? 'TRUE' : 'FALSE';
$timeout = abs((int)$timeout);
/** inside pl/sql scopes pdo binding not working correctly :( */
$this->db->createCommand(
'DECLARE
handle VARCHAR2(128);
BEGIN
DBMS_LOCK.ALLOCATE_UNIQUE(:name, handle);
:lockStatus := DBMS_LOCK.REQUEST(handle, DBMS_LOCK.' . $this->lockMode . ', ' . $timeout . ', ' . $releaseOnCommit . ');
END;',
[':name' => $name]
)
->bindParam(':lockStatus', $lockStatus, PDO::PARAM_INT, 1)
->execute();
return ($lockStatus === 0 || $lockStatus === '0');
}
/**
* Releases lock by given name.
* @param string $name of the lock to be released.
* @return boolean release result.
* @see http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/d_lock.htm
*/
protected function releaseLock($name)
{
$releaseStatus = null;
$this->db->createCommand(
'DECLARE
handle VARCHAR2(128);
BEGIN
DBMS_LOCK.ALLOCATE_UNIQUE(:name, handle);
:result := DBMS_LOCK.RELEASE(handle);
END;',
[':name' => $name]
)
->bindParam(':result', $releaseStatus, PDO::PARAM_INT, 1)
->execute();
return ($releaseStatus === 0 || $releaseStatus === '0');
}
}
Loading…
Cancel
Save