-
Notifications
You must be signed in to change notification settings - Fork 3
/
ReconnectingPDO.php
331 lines (294 loc) · 7.97 KB
/
ReconnectingPDO.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php
/*
* LegoW\ReconnectingPDO (https://github.com/adamturcsan/reconnecting-pdo)
*
* @copyright Copyright (c) 2014-2016 Legow Hosting Kft. (http://www.legow.hu)
* @license https://opensource.org/licenses/MIT MIT License
*/
namespace LegoW\ReconnectingPDO;
use PDO;
use LegoW\ReconnectingPDO\ReconnectingPDOStatement;
use LegoW\ReconnectingPDO\ReconnectingPDOException;
/**
* It covers the PDO database handler to prevent connection loss caused by non-critical
* error (ie. the MySQL has gone away).
*
* The default error handling is set to Exception instead \PDO::ERRMODE_SILENT.
*
* @author Turcsán Ádám <turcsan.adam@legow.hu>
*/
class ReconnectingPDO extends PDO
{
/**
* @var PDO
*/
protected $connection;
/**
* @var string DSN
*/
protected $dsn;
/**
* @var string Username
*/
protected $username;
/**
* @var string Password
*/
protected $passwd;
/**
* @var int[] PDO Options <br />
* Use original <b>PDO</b> constants
*/
protected $options;
/**
* @var int Reconnect counter
*/
protected $reconnectCounter;
/**
* @var int Maximum reconnection for one function call
*/
protected $maxReconnection;
/**
* (PHP 5 >= 5.1.3, PHP 7, PECL pdo >= 1.0.3)<br/>
* Return an array of available PDO drivers
* @link http://php.net/manual/en/pdo.getavailabledrivers.php
* @return array <b>PDO::getAvailableDrivers</b> returns an array of PDO driver names. If
* no drivers are available, it returns an empty array.
*/
public static function getAvailableDrivers()
{
return \PDO::getAvailableDrivers();
}
/**
* (PHP 5 >= 5.1.0, PECL pdo >= 0.1.0)<br/>
* Creates a ReconnectingPDO instance representing a connection to a database
* @link http://php.net/manual/en/pdo.construct.php
* @param $dsn
* @param $username [optional]
* @param $passwd [optional]
* @param $options [optional]
* @param $maxRetry [optional]
*/
public function __construct($dsn = null, $username = null, $passwd = null,
$options = null, $maxRetry = 5)
{
$this->dsn = $dsn;
$this->username = $username;
$this->passwd = $passwd;
$this->options = $options;
$this->maxReconnection = $maxRetry;
if ($this->dsn) {
$this->connectDb();
}
$this->resetCounter();
}
/**
* @param string $method
* @param array $arguments
* @return mixed
* @throws \PDOException
*/
protected function call($method, $arguments)
{
if (!($this->connection instanceof PDO)) {
throw new ReconnectingPDOException('No PDO connection is set');
}
try {
$this->ping($method);
$returnValue = call_user_func_array([$this->connection, $method],
$arguments);
} catch (\PDOException $ex) {
if (!stristr($ex->getMessage(), "server has gone away") || $ex->getCode() != 'HY000') {
throw $ex;
}
if ($this->reconnectCounter >= $this->maxReconnection) {
throw new ExceededMaxReconnectionException('ReconnectingPDO has exceeded max reconnection limit',
$ex->getCode(), $ex);
}
$this->reconnectDb();
$returnValue = $this->call($method, $arguments); // Retry
$this->resetCounter();
}
if ($returnValue instanceof \PDOStatement) {
return new ReconnectingPDOStatement($returnValue, $this,
$method === 'query');
}
return $returnValue;
}
protected function reconnectDb()
{
unset($this->connection);
$this->connectDb();
$this->reconnectCounter++;
}
protected function connectDb()
{
$this->connection = new PDO($this->dsn, $this->username, $this->passwd,
$this->options);
$this->connection->setAttribute(PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION);
}
/**
* If a function call didn't throw exception, reconnection counter can be reseted
*/
protected function resetCounter()
{
$this->reconnectCounter = 0;
}
/**
*
* @param int $max
*/
public function setMaxReconnection($max)
{
$this->maxReconnection = $max;
}
/**
*
* Parameters can be <b>(string)dsn</b>, <b>(string)username</b>,
* (string)<b>passwd</b> and <b>(array)options</b>
*
* @param array $parameters
* @param bool $autoconnect
*/
public function setConnectionParameters($parameters)
{
foreach ($parameters as $key => $param) {
if (property_exists($this, $key)) {
$this->$key = $param;
}
}
}
/**
* Prepares a statement for execution and returns a statement object
* @param string $statement
* @param array $driver_options [optional]
* @return ReconnectingPDOStatement|bool
*/
public function prepare($statement, $driver_options = [])
{
return $this->call('prepare', [$statement, $driver_options]);
}
/**
* {@inheritDoc}
*/
public function exec($statement)
{
return $this->call('exec', [$statement]);
}
/**
* {@inheritDoc}
*/
public function quote($string, $parameter_type = null)
{
return $this->call('quote', [$string, $parameter_type]);
}
/**
* {@inheritDoc}
*/
public function beginTransaction()
{
return $this->call('beginTransaction', []);
}
/**
* {@inheritDoc}
*/
public function rollBack()
{
return $this->call('rollBack', []);
}
/**
* {@inheritDoc}
*/
public function commit()
{
return $this->call('commit', []);
}
/**
* {@inheritDoc}
*/
public function errorCode()
{
return $this->call('errorCode', []);
}
/**
* {@inheritDoc}
*/
public function errorInfo()
{
return $this->call('errorInfo', []);
}
/**
* {@inheritDoc}
*/
public function lastInsertId($name = null)
{
return $this->call('lastInsertId', [$name]);
}
/**
* {@inheritDoc}
*/
public function getAttribute($attribute)
{
return $this->call('getAttribute', [$attribute]);
}
/**
* {@inheritDoc}
*/
public function setAttribute($attribute, $value)
{
return $this->call('setAttribute', [$attribute, $value]);
}
/**
* {@inheritDoc}
*/
public function inTransaction()
{
return $this->call('inTransaction', []);
}
/**
* Executes an SQL statement, returning a result set as a PDOStatement object
* @param string $statement The SQL statement to prepare and execute.
* @return ReconnectingPDOStatement|bool
*/
public function query($statement)
{
return $this->call('query', [$statement]);
}
public function setPDO(PDO $pdoObject)
{
if (!$this->connectionParametersAreSet()) {
throw new ConnectionParametersMissingException();
}
$this->connection = $pdoObject;
}
/**
* {@inheritDoc}
*/
public function sqliteCreateFunction($functionName, callable $callback, $num_args = -1)
{
return $this->call('sqliteCreateFunction', [$functionName, $callback, $num_args]);
}
protected function connectionParametersAreSet()
{
if ($this->dsn !== null && $this->username !== null && $this->passwd !== null) {
return true;
}
return false;
}
/**
*
* @param string $methodName
*/
protected function ping($methodName)
{
switch ($methodName) {
case 'lastInsertId':
//noop
break;
default:
$this->connection->query('SELECT 1');
break;
}
}
}