Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
connectionpool.php
1
<?php
9
namespace
Bitrix\Main\Data
;
10
11
use
Bitrix\Main
;
12
use
Bitrix\Main\Config
;
13
17
class
ConnectionPool
18
{
22
protected
$connections
= [];
23
24
protected
$connectionParameters
= [];
25
26
protected
$slavePossible
=
true
;
27
protected
$ignoreDml
= 0;
28
protected
$masterOnly
= 0;
29
protected
$slaveConnection
=
null
;
30
31
const
DEFAULT_CONNECTION_NAME
=
"default"
;
32
36
public
function
__construct
()
37
{
38
}
39
46
protected
function
createConnection
($name, $parameters)
47
{
48
$className = $parameters[
'className'
];
49
50
if
(!class_exists($className))
51
{
52
throw
new
Config\ConfigurationException
(sprintf(
53
"Class '%s' for '%s' connection was not found"
, $className, $name
54
));
55
}
56
57
$connection =
new
$className($parameters);
58
59
$this->connections[$name] = $connection;
60
61
return
$connection;
62
}
63
70
public
function
getConnection
($name =
""
)
71
{
72
if
($name ===
""
)
73
{
74
$name =
self::DEFAULT_CONNECTION_NAME
;
75
}
76
77
if
(!isset($this->connections[$name]))
78
{
79
$connParameters = $this->
getConnectionParameters
($name);
80
if
(!empty($connParameters) && is_array($connParameters))
81
{
82
$this->
createConnection
($name, $connParameters);
83
}
84
}
85
86
if
(isset($this->connections[$name]))
87
{
88
return
$this->connections[$name];
89
}
90
91
return
null
;
92
}
93
102
protected
function
getConnectionParameters
($name)
103
{
104
if
(!is_string($name))
105
{
106
throw
new
Main\ArgumentTypeException
(
"name"
,
"string"
);
107
}
108
109
if
($name ===
""
)
110
{
111
throw
new
Main\ArgumentNullException
(
"name"
);
112
}
113
114
$params =
null
;
115
if
(isset($this->connectionParameters[$name]) && !empty($this->connectionParameters[$name]))
116
{
117
$params = $this->connectionParameters[$name];
118
}
119
else
120
{
121
$configParams = Config\Configuration::getValue(
'connections'
);
122
if
(isset($configParams[$name]) && !empty($configParams[$name]))
123
{
124
$params = $configParams[$name];
125
}
126
}
127
128
if
($params !==
null
&& $name === static::DEFAULT_CONNECTION_NAME && !isset($params[
"include_after_connected"
]))
129
{
130
$params[
"include_after_connected"
] = \Bitrix\Main\Loader::getPersonal(
"php_interface/after_connect_d7.php"
);
131
}
132
133
return
$params;
134
}
135
143
public
function
setConnectionParameters
($name, $parameters)
144
{
145
$this->connectionParameters[$name] = $parameters;
146
147
if
(isset($this->connections[$name]))
148
{
149
unset($this->connections[$name]);
150
}
151
}
152
159
public
function
getSlaveConnection
($sql)
160
{
161
if
($this->masterOnly > 0)
162
{
163
//We requested to process all queries
164
//by master connection
165
}
166
elseif($this->slavePossible)
167
{
168
$isSelect = preg_match(
'/^\s*(select|show)/i'
, $sql) && !preg_match(
'/get_lock/i'
, $sql);
169
if
(!$isSelect && $this->
ignoreDml
<= 0)
170
{
171
$this->slavePossible =
false
;
172
}
173
174
if
($isSelect)
175
{
176
if
($this->slaveConnection ===
null
)
177
{
178
$this->
useMasterOnly
(
true
);
179
180
$this->slaveConnection = $this->
createSlaveConnection
();
181
182
$this->
useMasterOnly
(
false
);
183
}
184
185
if
(is_object($this->slaveConnection))
186
{
187
return
$this->slaveConnection
;
188
}
189
}
190
}
191
return
null
;
192
}
193
200
public
function
useMasterOnly
($mode)
201
{
202
if
($mode)
203
{
204
$this->masterOnly++;
205
}
206
else
207
{
208
$this->masterOnly--;
209
}
210
}
211
218
public
function
ignoreDml
($mode)
219
{
220
if
($mode)
221
{
222
$this->
ignoreDml
++;
223
}
224
else
225
{
226
$this->
ignoreDml
--;
227
}
228
}
229
235
protected
function
createSlaveConnection
()
236
{
237
if
(!Main\
Loader::includeModule
(
'cluster'
))
238
{
239
return
false
;
240
}
241
242
$found = \CClusterSlave::GetRandomNode();
243
244
if
($found !==
false
)
245
{
246
$node = \CClusterDBNode::GetByID($found[
"ID"
]);
247
248
if
(is_array($node) && $node[
"ACTIVE"
] ==
"Y"
&& ($node[
"STATUS"
] ==
"ONLINE"
|| $node[
"STATUS"
] ==
"READY"
))
249
{
250
$parameters = [
251
'host'
=> $node[
"DB_HOST"
],
252
'database'
=> $node[
"DB_NAME"
],
253
'login'
=> $node[
"DB_LOGIN"
],
254
'password'
=> $node[
"DB_PASSWORD"
],
255
];
256
257
$connection = $this->
cloneConnection
(self::DEFAULT_CONNECTION_NAME,
"node"
.$node[
"ID"
], $parameters);
258
259
if
($connection instanceof Main\DB\
Connection
)
260
{
261
$connection->setNodeId($node[
"ID"
]);
262
}
263
264
return
$connection;
265
}
266
}
267
return
false
;
268
}
269
279
public
function
cloneConnection
($name, $newName, array $parameters=array())
280
{
281
$defParameters = $this->
getConnectionParameters
($name);
282
if
(empty($defParameters) || !is_array($defParameters))
283
{
284
throw
new
Config\ConfigurationException
(sprintf(
"Database connection '%s' is not found"
, $name));
285
}
286
$parameters = array_merge($defParameters, $parameters);
287
288
$connection = $this->
createConnection
($newName, $parameters);
289
290
return
$connection;
291
}
292
298
public
function
isSlavePossible
()
299
{
300
return
$this->slavePossible
;
301
}
302
308
public
function
isMasterOnly
()
309
{
310
return
($this->masterOnly > 0);
311
}
312
316
public
function
disconnect
()
317
{
318
foreach
($this->connections as $connection)
319
{
320
if
($connection instanceof Main\DB\
Connection
)
321
{
322
$connection->disconnect();
323
}
324
}
325
}
326
}
Bitrix\Main\ArgumentNullException
Definition
exception.php:54
Bitrix\Main\ArgumentTypeException
Definition
exception.php:114
Bitrix\Main\Config\ConfigurationException
Definition
configurationexception.php:9
Bitrix\Main\Data\Connection
Definition
connection.php:17
Bitrix\Main\Data\ConnectionPool
Definition
connectionpool.php:18
Bitrix\Main\Data\ConnectionPool\__construct
__construct()
Definition
connectionpool.php:36
Bitrix\Main\Data\ConnectionPool\isMasterOnly
isMasterOnly()
Definition
connectionpool.php:308
Bitrix\Main\Data\ConnectionPool\getSlaveConnection
getSlaveConnection($sql)
Definition
connectionpool.php:159
Bitrix\Main\Data\ConnectionPool\getConnectionParameters
getConnectionParameters($name)
Definition
connectionpool.php:102
Bitrix\Main\Data\ConnectionPool\DEFAULT_CONNECTION_NAME
const DEFAULT_CONNECTION_NAME
Definition
connectionpool.php:31
Bitrix\Main\Data\ConnectionPool\isSlavePossible
isSlavePossible()
Definition
connectionpool.php:298
Bitrix\Main\Data\ConnectionPool\$connectionParameters
$connectionParameters
Definition
connectionpool.php:24
Bitrix\Main\Data\ConnectionPool\ignoreDml
ignoreDml($mode)
Definition
connectionpool.php:218
Bitrix\Main\Data\ConnectionPool\getConnection
getConnection($name="")
Definition
connectionpool.php:70
Bitrix\Main\Data\ConnectionPool\$connections
$connections
Definition
connectionpool.php:22
Bitrix\Main\Data\ConnectionPool\createSlaveConnection
createSlaveConnection()
Definition
connectionpool.php:235
Bitrix\Main\Data\ConnectionPool\setConnectionParameters
setConnectionParameters($name, $parameters)
Definition
connectionpool.php:143
Bitrix\Main\Data\ConnectionPool\useMasterOnly
useMasterOnly($mode)
Definition
connectionpool.php:200
Bitrix\Main\Data\ConnectionPool\disconnect
disconnect()
Definition
connectionpool.php:316
Bitrix\Main\Data\ConnectionPool\cloneConnection
cloneConnection($name, $newName, array $parameters=array())
Definition
connectionpool.php:279
Bitrix\Main\Data\ConnectionPool\$ignoreDml
$ignoreDml
Definition
connectionpool.php:27
Bitrix\Main\Data\ConnectionPool\$masterOnly
$masterOnly
Definition
connectionpool.php:28
Bitrix\Main\Data\ConnectionPool\createConnection
createConnection($name, $parameters)
Definition
connectionpool.php:46
Bitrix\Main\Data\ConnectionPool\$slavePossible
$slavePossible
Definition
connectionpool.php:26
Bitrix\Main\Data\ConnectionPool\$slaveConnection
$slaveConnection
Definition
connectionpool.php:29
Bitrix\Main\Loader\includeModule
static includeModule($moduleName)
Definition
loader.php:69
Bitrix\Main\Config
Definition
configuration.php:2
Bitrix\Main\Data
Definition
aliases.php:105
Bitrix\Main
modules
main
lib
data
connectionpool.php
Создано системой
1.10.0