MysqlMutex.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. namespace yii\mutex;
  8. use yii\base\InvalidConfigException;
  9. /**
  10. * MysqlMutex implements mutex "lock" mechanism via MySQL locks.
  11. *
  12. * Application configuration example:
  13. *
  14. * ```
  15. * [
  16. * 'components' => [
  17. * 'db' => [
  18. * 'class' => 'yii\db\Connection',
  19. * 'dsn' => 'mysql:host=127.0.0.1;dbname=demo',
  20. * ]
  21. * 'mutex' => [
  22. * 'class' => 'yii\mutex\MysqlMutex',
  23. * ],
  24. * ],
  25. * ]
  26. * ```
  27. *
  28. * @see Mutex
  29. *
  30. * @author resurtm <resurtm@gmail.com>
  31. * @since 2.0
  32. */
  33. class MysqlMutex extends DbMutex
  34. {
  35. /**
  36. * Initializes MySQL specific mutex component implementation.
  37. * @throws InvalidConfigException if [[db]] is not MySQL connection.
  38. */
  39. public function init()
  40. {
  41. parent::init();
  42. if ($this->db->driverName !== 'mysql') {
  43. throw new InvalidConfigException('In order to use MysqlMutex connection must be configured to use MySQL database.');
  44. }
  45. }
  46. /**
  47. * Acquires lock by given name.
  48. * @param string $name of the lock to be acquired.
  49. * @param int $timeout to wait for lock to become released.
  50. * @return bool acquiring result.
  51. * @see http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_get-lock
  52. */
  53. protected function acquireLock($name, $timeout = 0)
  54. {
  55. return (bool) $this->db
  56. ->createCommand('SELECT GET_LOCK(:name, :timeout)', [':name' => $name, ':timeout' => $timeout])
  57. ->queryScalar();
  58. }
  59. /**
  60. * Releases lock by given name.
  61. * @param string $name of the lock to be released.
  62. * @return bool release result.
  63. * @see http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_release-lock
  64. */
  65. protected function releaseLock($name)
  66. {
  67. return (bool) $this->db
  68. ->createCommand('SELECT RELEASE_LOCK(:name)', [':name' => $name])
  69. ->queryScalar();
  70. }
  71. }