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.
58 lines
1.5 KiB
58 lines
1.5 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
|
|
||
12 years ago
|
namespace yii\mutex;
|
||
12 years ago
|
|
||
|
use Yii;
|
||
|
use yii\base\InvalidConfigException;
|
||
|
|
||
12 years ago
|
/**
|
||
|
* @author resurtm <resurtm@gmail.com>
|
||
|
* @since 2.0
|
||
|
*/
|
||
12 years ago
|
class MysqlMutex extends Mutex
|
||
12 years ago
|
{
|
||
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') {
|
||
12 years ago
|
throw new InvalidConfigException('In order to use MysqlMutex connection must be configured to use MySQL database.');
|
||
12 years ago
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
11 years ago
|
* Acquires lock by given name.
|
||
12 years ago
|
* @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 acquireLock($name, $timeout = 0)
|
||
12 years ago
|
{
|
||
|
return (boolean)$this->db
|
||
11 years ago
|
->createCommand('SELECT GET_LOCK(:name, :timeout)', [':name' => $name, ':timeout' => $timeout])
|
||
12 years ago
|
->queryScalar();
|
||
|
}
|
||
|
|
||
12 years ago
|
/**
|
||
11 years ago
|
* Releases lock by given name.
|
||
12 years ago
|
* @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 releaseLock($name)
|
||
12 years ago
|
{
|
||
|
return (boolean)$this->db
|
||
11 years ago
|
->createCommand('SELECT RELEASE_LOCK(:name)', [':name' => $name])
|
||
12 years ago
|
->queryScalar();
|
||
|
}
|
||
|
}
|