@@ -77,6 +77,7 @@ class Connection extends \Doctrine\DBAL\Connection {
7777
7878 /** @var DbDataCollector|null */
7979 protected $ dbDataCollector = null ;
80+ private int $ lastConnectionCheck = 0 ;
8081
8182 /**
8283 * Initializes a new instance of the Connection class.
@@ -122,10 +123,13 @@ public function __construct(
122123 public function connect () {
123124 try {
124125 if ($ this ->_conn ) {
126+ $ this ->reconnectIfNeeded ();
125127 /** @psalm-suppress InternalMethod */
126128 return parent ::connect ();
127129 }
128130
131+ $ this ->lastConnectionCheck = time ();
132+
129133 // Only trigger the event logger for the initial connect call
130134 $ eventLogger = \OC ::$ server ->get (IEventLogger::class);
131135 $ eventLogger ->start ('connect:db ' , 'db connection opened ' );
@@ -603,4 +607,21 @@ private function getMigrator() {
603607 return new Migrator ($ this , $ config , $ dispatcher );
604608 }
605609 }
610+
611+ private function reconnectIfNeeded (): void {
612+ if ($ this ->lastConnectionCheck + 30 >= time ()) {
613+ return ;
614+ }
615+
616+ try {
617+ $ this ->_conn ->query ($ this ->getDriver ()->getDatabasePlatform ()->getDummySelectSQL ());
618+ $ this ->lastConnectionCheck = time ();
619+ } catch (Exception \ConnectionLost $ e ) {
620+ $ this ->logger ->warning ('Exception during connectivity check, closing and reconnecting ' , ['exception ' => $ e ]);
621+ $ this ->close ();
622+ } catch (\Exception $ e ) {
623+ $ this ->logger ->warning ('Exception during connectivity check, closing and reconnecting ' , ['exception ' => $ e ]);
624+ $ this ->close ();
625+ }
626+ }
606627}
0 commit comments