You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
2.2 KiB
73 lines
2.2 KiB
12 years ago
|
<?php
|
||
12 years ago
|
/**
|
||
|
* @link http://www.yiiframework.com/
|
||
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
12 years ago
|
|
||
|
namespace yii\mutex\db\mysql;
|
||
|
|
||
|
use Yii;
|
||
|
use yii\base\InvalidConfigException;
|
||
|
|
||
12 years ago
|
/**
|
||
|
* @author resurtm <resurtm@gmail.com>
|
||
|
* @since 2.0
|
||
|
*/
|
||
12 years ago
|
class Mutex extends \yii\mutex\db\Mutex
|
||
|
{
|
||
12 years ago
|
/**
|
||
|
* Initializes MySQL specific mutex component implementation.
|
||
|
* @throws InvalidConfigException if [[db]] is not MySQL connection.
|
||
|
*/
|
||
12 years ago
|
public function init()
|
||
|
{
|
||
|
parent::init();
|
||
|
if ($this->db->driverName !== 'mysql') {
|
||
|
throw new InvalidConfigException('');
|
||
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
|
* This method should be extended by concrete mutex implementations. Acquires lock by given name.
|
||
|
* @param string $name of the lock to be acquired.
|
||
|
* @param integer $timeout to wait for lock to become released.
|
||
|
* @return boolean acquiring result.
|
||
|
* @see http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_get-lock
|
||
|
*/
|
||
12 years ago
|
protected function acquire($name, $timeout = 0)
|
||
|
{
|
||
|
return (boolean)$this->db
|
||
|
->createCommand('SELECT GET_LOCK(:name, :timeout)', array(':name' => $name, ':timeout' => $timeout))
|
||
|
->queryScalar();
|
||
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
|
* This method should be extended by concrete mutex implementations. Releases lock by given name.
|
||
|
* @param string $name of the lock to be released.
|
||
|
* @return boolean release result.
|
||
|
* @see http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_release-lock
|
||
|
*/
|
||
12 years ago
|
protected function release($name)
|
||
|
{
|
||
|
return (boolean)$this->db
|
||
|
->createCommand('SELECT RELEASE_LOCK(:name)', array(':name' => $name))
|
||
|
->queryScalar();
|
||
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
|
* This method may optionally be extended by concrete mutex implementations. Checks whether lock has been
|
||
|
* already acquired by given name.
|
||
|
* @param string $name of the lock to be released.
|
||
|
* @return null|boolean whether lock has been already acquired. Returns `null` in case this feature
|
||
|
* is not supported by concrete mutex implementation.
|
||
|
* @see http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_is-free-lock
|
||
|
*/
|
||
12 years ago
|
protected function getIsAcquired($name)
|
||
|
{
|
||
|
return (boolean)$this->db
|
||
|
->createCommand('SELECT IS_FREE_LOCK(:name)', array(':name' => $name))
|
||
|
->queryScalar();
|
||
|
}
|
||
|
}
|