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
			| 
								 
											13 years ago
										 
									 | 
							
								<?php
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * @link http://www.yiiframework.com/
							 | 
						||
| 
								 | 
							
								 * @copyright Copyright (c) 2008 Yii Software LLC
							 | 
						||
| 
								 | 
							
								 * @license http://www.yiiframework.com/license/
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								namespace yii\mutex;
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								use Yii;
							 | 
						||
| 
								 | 
							
								use yii\base\InvalidConfigException;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * @author resurtm <resurtm@gmail.com>
							 | 
						||
| 
								 | 
							
								 * @since 2.0
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								class MysqlMutex extends Mutex
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
								{
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									/**
							 | 
						||
| 
								 | 
							
									 * Initializes MySQL specific mutex component implementation.
							 | 
						||
| 
								 | 
							
									 * @throws InvalidConfigException if [[db]] is not MySQL connection.
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									public function init()
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 | 
							
										parent::init();
							 | 
						||
| 
								 | 
							
										if ($this->db->driverName !== 'mysql') {
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											throw new InvalidConfigException('In order to use MysqlMutex connection must be configured to use MySQL database.');
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
										}
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									/**
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * Acquires lock by given name.
							 | 
						||
| 
								 
											13 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
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									protected function acquireLock($name, $timeout = 0)
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									{
							 | 
						||
| 
								 | 
							
										return (boolean)$this->db
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
											->createCommand('SELECT GET_LOCK(:name, :timeout)', [':name' => $name, ':timeout' => $timeout])
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											->queryScalar();
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									/**
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
									 * Releases lock by given name.
							 | 
						||
| 
								 
											13 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
							 | 
						||
| 
								 | 
							
									 */
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									protected function releaseLock($name)
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
									{
							 | 
						||
| 
								 | 
							
										return (boolean)$this->db
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
											->createCommand('SELECT RELEASE_LOCK(:name)', [':name' => $name])
							 | 
						||
| 
								 
											13 years ago
										 
									 | 
							
											->queryScalar();
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								}
							 |