1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
connectionpool.php
См. документацию.
1<?php
2
9
10namespace Bitrix\Main\Data;
11
12use Bitrix\Main;
13use Bitrix\Main\DB;
14use Bitrix\Main\Config;
15
20{
21 const DEFAULT_CONNECTION_NAME = "default";
22
26 protected $connections = [];
27 protected $connectionParameters = [];
28 protected $slavePossible = true;
29 protected $ignoreDml = 0;
30 protected $masterOnly = 0;
31 protected $slaveConnection = null;
32 protected array $modifiedTables = [];
33
37 public function __construct()
38 {
39 }
40
47 protected function createConnection($name, $parameters)
48 {
49 $className = $parameters['className'];
50
51 if (isset($parameters['module']))
52 {
53 \Bitrix\Main\Loader::includeModule($parameters['module']);
54 }
55
56 if (!class_exists($className))
57 {
58 throw new Config\ConfigurationException(sprintf(
59 "Class '%s' for '%s' connection was not found", $className, $name
60 ));
61 }
62
63 $connection = new $className($parameters);
64
65 $this->connections[$name] = $connection;
66
67 return $connection;
68 }
69
76 public function getConnection($name = "")
77 {
78 if ($name === "")
79 {
80 $name = self::DEFAULT_CONNECTION_NAME;
81 }
82
83 if (!isset($this->connections[$name]))
84 {
85 $connParameters = $this->getConnectionParameters($name);
86 if (!empty($connParameters) && is_array($connParameters))
87 {
88 $this->createConnection($name, $connParameters);
89 }
90 }
91
92 if (isset($this->connections[$name]))
93 {
94 return $this->connections[$name];
95 }
96
97 return null;
98 }
99
107 protected function getConnectionParameters(string $name)
108 {
109 if ($name === "")
110 {
111 throw new Main\ArgumentNullException("name");
112 }
113
114 $params = null;
115 if (!empty($this->connectionParameters[$name]))
116 {
117 $params = $this->connectionParameters[$name];
118 }
119 else
120 {
121 $configParams = Config\Configuration::getValue('connections');
122 if (!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"] = 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 by master connection.
164 return null;
165 }
166
167 $this->useMasterOnly(true);
168 $cluster = Main\ModuleManager::isModuleInstalled('cluster');
169 $this->useMasterOnly(false);
170
171 if (!$cluster)
172 {
173 // Slave connection is impossible, cluster module is not installed.
174 return null;
175 }
176
177 $isSelect = preg_match('/^\s*(select|show)/i', $sql) && !preg_match('/get_lock/i', $sql);
178
179 if (!$isSelect && $this->ignoreDml <= 0)
180 {
181 $tables = $this->getConnection()->getSqlHelper()->getQueryTables($sql, 0);
182 foreach ($tables as $table)
183 {
184 $this->modifiedTables[$table] = 1;
185 }
186
187 $this->slavePossible = false;
188 }
189 elseif ($isSelect)
190 {
191 if (!$this->slavePossible && !empty($this->modifiedTables))
192 {
193 $tables = $this->getConnection()->getSqlHelper()->getQueryTables($sql);
194 foreach ($tables as $table)
195 {
196 if (isset($this->modifiedTables[$table]))
197 {
198 return null;
199 }
200 }
201 }
202
203 if ($this->slaveConnection === null)
204 {
205 $this->useMasterOnly(true);
206 $this->slaveConnection = $this->createSlaveConnection();
207 $this->useMasterOnly(false);
208 }
209
210 if (is_object($this->slaveConnection))
211 {
213 }
214 }
215 return null;
216 }
217
224 public function useMasterOnly($mode)
225 {
226 if ($mode)
227 {
228 $this->masterOnly++;
229 }
230 else
231 {
232 $this->masterOnly--;
233 }
234 }
235
242 public function ignoreDml($mode)
243 {
244 if ($mode)
245 {
246 $this->ignoreDml++;
247 }
248 else
249 {
250 $this->ignoreDml--;
251 }
252 }
253
259 protected function createSlaveConnection()
260 {
261 if (!Main\Loader::includeModule('cluster'))
262 {
263 return false;
264 }
265
267
268 if ($found !== false)
269 {
270 $node = \CClusterDBNode::GetByID($found["ID"]);
271
272 if (is_array($node) && $node["ACTIVE"] == "Y" && ($node["STATUS"] == "ONLINE" || $node["STATUS"] == "READY"))
273 {
274 $parameters = [
275 'host' => $node["DB_HOST"],
276 'database' => $node["DB_NAME"],
277 'login' => $node["DB_LOGIN"],
278 'password' => $node["DB_PASSWORD"],
279 ];
280
281 $connection = $this->cloneConnection(self::DEFAULT_CONNECTION_NAME, "node" . $node["ID"], $parameters);
282
283 if ($connection instanceof DB\Connection)
284 {
285 $connection->setNodeId($node["ID"]);
286 }
287
288 return $connection;
289 }
290 }
291 return false;
292 }
293
303 public function cloneConnection($name, $newName, array $parameters = [])
304 {
305 $defParameters = $this->getConnectionParameters($name);
306 if (empty($defParameters) || !is_array($defParameters))
307 {
308 throw new Config\ConfigurationException(sprintf("Database connection '%s' is not found", $name));
309 }
310 $parameters = array_merge($defParameters, $parameters);
311
312 $connection = $this->createConnection($newName, $parameters);
313
314 return $connection;
315 }
316
322 public function isSlavePossible()
323 {
325 }
326
332 public function isMasterOnly()
333 {
334 return ($this->masterOnly > 0);
335 }
336
340 public function disconnect()
341 {
342 foreach ($this->connections as $connection)
343 {
344 if ($connection instanceof DB\Connection)
345 {
346 $connection->disconnect();
347 }
348 }
349 }
350}
$connection
Определения actionsdefinitions.php:38
static getValue($name)
Определения configuration.php:24
getConnectionParameters(string $name)
Определения connectionpool.php:107
const DEFAULT_CONNECTION_NAME
Определения connectionpool.php:21
getConnection($name="")
Определения connectionpool.php:76
setConnectionParameters($name, $parameters)
Определения connectionpool.php:143
cloneConnection($name, $newName, array $parameters=[])
Определения connectionpool.php:303
createConnection($name, $parameters)
Определения connectionpool.php:47
static includeModule($moduleName)
Определения loader.php:67
static getPersonal($path)
Определения loader.php:600
static isModuleInstalled($moduleName)
Определения modulemanager.php:125
static GetByID($node_id, $arVirtNode=false)
Определения dbnode.php:79
static GetRandomNode()
Определения slave.php:583
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$name
Определения menu_edit.php:35
Определения arrayresult.php:2
$table
Определения mysql_to_pgsql.php:36
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799