Completed
Push — fix-unique-exists-expressions ( b0719b...8467f4 )
by Alexander
07:52
created

MysqlMutex::releaseLock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\mutex;
9
10
use yii\base\InvalidConfigException;
11
12
/**
13
 * MysqlMutex implements mutex "lock" mechanism via MySQL locks.
14
 *
15
 * Application configuration example:
16
 *
17
 * ```
18
 * [
19
 *     'components' => [
20
 *         'db' => [
21
 *             'class' => 'yii\db\Connection',
22
 *             'dsn' => 'mysql:host=127.0.0.1;dbname=demo',
23
 *         ]
24
 *         'mutex' => [
25
 *             'class' => 'yii\mutex\MysqlMutex',
26
 *         ],
27
 *     ],
28
 * ]
29
 * ```
30
 *
31
 * @see Mutex
32
 *
33
 * @author resurtm <[email protected]>
34
 * @since 2.0
35
 */
36
class MysqlMutex extends DbMutex
37
{
38
    /**
39
     * Initializes MySQL specific mutex component implementation.
40
     * @throws InvalidConfigException if [[db]] is not MySQL connection.
41
     */
42
    public function init()
43
    {
44
        parent::init();
45
        if ($this->db->driverName !== 'mysql') {
46
            throw new InvalidConfigException('In order to use MysqlMutex connection must be configured to use MySQL database.');
47
        }
48
    }
49
50
    /**
51
     * Acquires lock by given name.
52
     * @param string $name of the lock to be acquired.
53
     * @param int $timeout to wait for lock to become released.
54
     * @return bool acquiring result.
55
     * @see http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_get-lock
56
     */
57
    protected function acquireLock($name, $timeout = 0)
58
    {
59
        return (bool) $this->db
60
            ->createCommand('SELECT GET_LOCK(:name, :timeout)', [':name' => $name, ':timeout' => $timeout])
61
            ->queryScalar();
62
    }
63
64
    /**
65
     * Releases lock by given name.
66
     * @param string $name of the lock to be released.
67
     * @return bool release result.
68
     * @see http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_release-lock
69
     */
70
    protected function releaseLock($name)
71
    {
72
        return (bool) $this->db
73
            ->createCommand('SELECT RELEASE_LOCK(:name)', [':name' => $name])
74
            ->queryScalar();
75
    }
76
}
77