Skip to content

Commit 7992e1e

Browse files
committed
Add DB name as lock prefix
1 parent c2e5520 commit 7992e1e

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

lib/internal/Magento/Framework/Lock/Backend/Database.php

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
declare(strict_types=1);
88
namespace Magento\Framework\Lock\Backend;
99

10+
use Magento\Framework\App\DeploymentConfig;
1011
use Magento\Framework\App\ResourceConnection;
12+
use Magento\Framework\Config\ConfigOptionsListConstants;
1113
use Magento\Framework\Exception\InputException;
1214
use Magento\Framework\Phrase;
1315

@@ -16,10 +18,20 @@ class Database implements \Magento\Framework\Lock\LockManagerInterface
1618
/** @var ResourceConnection */
1719
private $resource;
1820

21+
/** @var DeploymentConfig */
22+
private $deploymentConfig;
23+
24+
/** @var string Lock prefix */
25+
private $prefix;
26+
1927
public function __construct(
20-
ResourceConnection $resource
28+
ResourceConnection $resource,
29+
DeploymentConfig $deploymentConfig,
30+
string $prefix = null
2131
) {
2232
$this->resource = $resource;
33+
$this->deploymentConfig = $deploymentConfig;
34+
$this->prefix = $prefix;
2335
}
2436

2537
/**
@@ -32,7 +44,7 @@ public function __construct(
3244
*/
3345
public function acquireLock(string $name, int $timeout = -1): bool
3446
{
35-
$this->checkLength($name);
47+
$name = $this->addPrefix($name);
3648

3749
return (bool)$this->resource->getConnection()->query("SELECT GET_LOCK(?, ?);", [(string)$name, (int)$timeout])
3850
->fetchColumn();
@@ -47,7 +59,7 @@ public function acquireLock(string $name, int $timeout = -1): bool
4759
*/
4860
public function releaseLock(string $name): bool
4961
{
50-
$this->checkLength($name);
62+
$name = $this->addPrefix($name);
5163

5264
return (bool)$this->resource->getConnection()->query("SELECT RELEASE_LOCK(?);", [(string)$name])->fetchColumn();
5365
}
@@ -61,23 +73,45 @@ public function releaseLock(string $name): bool
6173
*/
6274
public function isLocked(string $name): bool
6375
{
64-
$this->checkLength($name);
76+
$name = $this->addPrefix($name);
6577

6678
return (bool)$this->resource->getConnection()->query("SELECT IS_USED_LOCK(?);", [(string)$name])->fetchColumn();
6779
}
6880

6981
/**
70-
* Checks for max length of lock name
82+
* Adds prefix and checks for max length of lock name
7183
*
7284
* Limited to 64 characters in MySQL.
7385
*
7486
* @param string $name
87+
* @return string $name
7588
* @throws InputException
7689
*/
77-
private function checkLength(string $name)
90+
private function addPrefix(string $name): string
7891
{
92+
$name = $this->getPrefix() . '|' . $name;
93+
7994
if (strlen($name) > 64) {
8095
throw new InputException(new Phrase('Lock name too long'));
8196
}
97+
98+
return $name;
99+
}
100+
101+
/**
102+
* Get installation specific lock prefix to avoid lock conflicts
103+
*
104+
* @return string lock prefix
105+
*/
106+
private function getPrefix(): string
107+
{
108+
if ($this->prefix === null) {
109+
$this->prefix = $this->deploymentConfig->get(
110+
ConfigOptionsListConstants::CONFIG_PATH_DB_CONNECTION_DEFAULT . '/' . ConfigOptionsListConstants::KEY_NAME,
111+
''
112+
);
113+
}
114+
115+
return $this->prefix;
82116
}
83117
}

0 commit comments

Comments
 (0)