17
17
use Doctrine \DBAL \Exception \ConnectionLost ;
18
18
use Doctrine \DBAL \Exception \DriverException ;
19
19
use Doctrine \DBAL \Exception \InvalidArgumentException ;
20
+ use Doctrine \DBAL \Exception as DBALException ;
20
21
use Doctrine \DBAL \Platforms \AbstractPlatform ;
21
22
use Doctrine \DBAL \Query \Expression \ExpressionBuilder ;
22
23
use Doctrine \DBAL \Query \QueryBuilder ;
@@ -166,6 +167,12 @@ class Connection
166
167
167
168
private SchemaManagerFactory $ schemaManagerFactory ;
168
169
170
+ private static bool $ isChecking = false ;
171
+
172
+ private static ?int $ lastCheckedAt = null ;
173
+
174
+ private ?int $ checkFrequency ;
175
+
169
176
/**
170
177
* Initializes a new instance of the Connection class.
171
178
*
@@ -186,6 +193,7 @@ public function __construct(
186
193
?Configuration $ config = null ,
187
194
?EventManager $ eventManager = null
188
195
) {
196
+ $ this ->checkFrequency = $ params ['check_connection_frequency ' ] ?? null ;
189
197
$ this ->_driver = $ driver ;
190
198
$ this ->params = $ params ;
191
199
@@ -371,6 +379,21 @@ public function connect()
371
379
);
372
380
373
381
if ($ this ->_conn !== null ) {
382
+ if (null === $ this ->checkFrequency ) {
383
+ return false ;
384
+ }
385
+
386
+ if (! self ::$ isChecking ) {
387
+ if (null === self ::$ lastCheckedAt || time () - self ::$ lastCheckedAt >= $ this ->checkFrequency ) {
388
+ self ::$ isChecking = true ;
389
+
390
+ $ this ->reconnectOnFailure ();
391
+
392
+ self ::$ lastCheckedAt = time ();
393
+ self ::$ isChecking = false ;
394
+ }
395
+ }
396
+
374
397
return false ;
375
398
}
376
399
@@ -2002,4 +2025,15 @@ public function exec(string $sql): int
2002
2025
2003
2026
return $ this ->executeStatement ($ sql );
2004
2027
}
2028
+
2029
+ private function reconnectOnFailure ()
2030
+ {
2031
+ try {
2032
+ $ this ->executeQuery ($ this ->getDatabasePlatform ($ this )->getDummySelectSQL ());
2033
+ } catch (DBALException ) {
2034
+ $ this ->close ();
2035
+ // Attempt to reestablish the lazy connection by sending another query.
2036
+ $ this ->executeQuery ($ this ->getDatabasePlatform ($ this )->getDummySelectSQL ());
2037
+ }
2038
+ }
2005
2039
}
0 commit comments