7
7
declare (strict_types=1 );
8
8
namespace Magento \Framework \Lock \Backend ;
9
9
10
+ use Magento \Framework \App \DeploymentConfig ;
10
11
use Magento \Framework \App \ResourceConnection ;
12
+ use Magento \Framework \Config \ConfigOptionsListConstants ;
11
13
use Magento \Framework \Exception \InputException ;
12
14
use Magento \Framework \Phrase ;
13
15
@@ -16,10 +18,20 @@ class Database implements \Magento\Framework\Lock\LockManagerInterface
16
18
/** @var ResourceConnection */
17
19
private $ resource ;
18
20
21
+ /** @var DeploymentConfig */
22
+ private $ deploymentConfig ;
23
+
24
+ /** @var string Lock prefix */
25
+ private $ prefix ;
26
+
19
27
public function __construct (
20
- ResourceConnection $ resource
28
+ ResourceConnection $ resource ,
29
+ DeploymentConfig $ deploymentConfig ,
30
+ string $ prefix = null
21
31
) {
22
32
$ this ->resource = $ resource ;
33
+ $ this ->deploymentConfig = $ deploymentConfig ;
34
+ $ this ->prefix = $ prefix ;
23
35
}
24
36
25
37
/**
@@ -32,7 +44,7 @@ public function __construct(
32
44
*/
33
45
public function acquireLock (string $ name , int $ timeout = -1 ): bool
34
46
{
35
- $ this ->checkLength ($ name );
47
+ $ name = $ this ->addPrefix ($ name );
36
48
37
49
return (bool )$ this ->resource ->getConnection ()->query ("SELECT GET_LOCK(?, ?); " , [(string )$ name , (int )$ timeout ])
38
50
->fetchColumn ();
@@ -47,7 +59,7 @@ public function acquireLock(string $name, int $timeout = -1): bool
47
59
*/
48
60
public function releaseLock (string $ name ): bool
49
61
{
50
- $ this ->checkLength ($ name );
62
+ $ name = $ this ->addPrefix ($ name );
51
63
52
64
return (bool )$ this ->resource ->getConnection ()->query ("SELECT RELEASE_LOCK(?); " , [(string )$ name ])->fetchColumn ();
53
65
}
@@ -61,23 +73,45 @@ public function releaseLock(string $name): bool
61
73
*/
62
74
public function isLocked (string $ name ): bool
63
75
{
64
- $ this ->checkLength ($ name );
76
+ $ name = $ this ->addPrefix ($ name );
65
77
66
78
return (bool )$ this ->resource ->getConnection ()->query ("SELECT IS_USED_LOCK(?); " , [(string )$ name ])->fetchColumn ();
67
79
}
68
80
69
81
/**
70
- * Checks for max length of lock name
82
+ * Adds prefix and checks for max length of lock name
71
83
*
72
84
* Limited to 64 characters in MySQL.
73
85
*
74
86
* @param string $name
87
+ * @return string $name
75
88
* @throws InputException
76
89
*/
77
- private function checkLength (string $ name )
90
+ private function addPrefix (string $ name ): string
78
91
{
92
+ $ name = $ this ->getPrefix () . '| ' . $ name ;
93
+
79
94
if (strlen ($ name ) > 64 ) {
80
95
throw new InputException (new Phrase ('Lock name too long ' ));
81
96
}
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 ;
82
116
}
83
117
}
0 commit comments