Skip to content

Commit

Permalink
fix(Pp61s0q6): no reconnect on lost DB connection
Browse files Browse the repository at this point in the history
  • Loading branch information
misha-rollun committed Jul 17, 2024
1 parent 5e58458 commit afedc33
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/DataStore/src/DataStore/DbTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ public function create($itemData, $rewriteIfExist = false)
$adapter->getDriver()->getConnection()->commit();
} catch (\Throwable $e) {
$adapter->getDriver()->getConnection()->rollback();
// https://github.com/laminas/laminas-db/issues/56
$adapter->getDriver()->getConnection()->disconnect();
$logContext = [
self::LOG_METHOD => __METHOD__,
self::LOG_TABLE => $this->dbTable->getTable(),
Expand Down Expand Up @@ -176,6 +178,8 @@ public function update($itemData, $createIfAbsent = false)
$adapter->getDriver()->getConnection()->commit();
} catch (\Throwable $e) {
$adapter->getDriver()->getConnection()->rollback();
// https://github.com/laminas/laminas-db/issues/56
$adapter->getDriver()->getConnection()->disconnect();
$logContext = [
self::LOG_METHOD => __METHOD__,
self::LOG_TABLE => $this->dbTable->getTable(),
Expand Down Expand Up @@ -228,6 +232,8 @@ public function queriedDelete(Query $query)
$statement = $adapter->getDriver()->createStatement($sqlString);
$result = $statement->execute();
} catch (\Throwable $e) {
// https://github.com/laminas/laminas-db/issues/56
$adapter->getDriver()->getConnection()->disconnect();
throw new DataStoreException("[{$this->dbTable->getTable()}]Can't delete records using query", 0, $e);
}

Expand Down Expand Up @@ -431,6 +437,8 @@ public function query(Query $query)
} catch (\Throwable $e) {
$logContext['exception'] = $e;
$this->writeLogsIfNeeded($logContext, "Request to db table '{$this->dbTable->getTable()}' failed");
// https://github.com/laminas/laminas-db/issues/56
$adapter->getDriver()->getConnection()->disconnect();
if (LaminasDbExceptionDetector::isConnectionException($e)) {
throw new ConnectionException($e->getMessage(), $e->getCode(), $e);
}
Expand Down Expand Up @@ -483,6 +491,8 @@ public function delete($id)
} catch (\Throwable $e) {
$logContext['exception'] = $e;
$this->writeLogsIfNeeded($logContext, "Request to db table '{$this->dbTable->getTable()}' failed");
// https://github.com/laminas/laminas-db/issues/56
$this->dbTable->getAdapter()->getDriver()->getConnection()->disconnect();
if (LaminasDbExceptionDetector::isConnectionException($e)) {
throw new ConnectionException($e->getMessage(), $e->getCode(), $e);
}
Expand Down Expand Up @@ -523,6 +533,8 @@ public function read($id)
} catch (\Throwable $e) {
$logContext['exception'] = $e;
$this->writeLogsIfNeeded($logContext, "Request to db table '{$this->dbTable->getTable()}' failed");
// https://github.com/laminas/laminas-db/issues/56
$this->dbTable->getAdapter()->getDriver()->getConnection()->disconnect();
if (LaminasDbExceptionDetector::isConnectionException($e)) {
throw new ConnectionException($e->getMessage(), $e->getCode(), $e);
}
Expand Down Expand Up @@ -556,6 +568,8 @@ public function deleteAll()
try {
return $this->dbTable->delete($where);
} catch (RuntimeException $e) {
// https://github.com/laminas/laminas-db/issues/56
$this->dbTable->getAdapter()->getDriver()->getConnection()->disconnect();
if (LaminasDbExceptionDetector::isConnectionException($e)) {
throw new ConnectionException($e->getMessage(), $e->getCode(), $e);
}
Expand All @@ -578,6 +592,8 @@ public function count()
try {
$statement = $adapter->getDriver()->createStatement($sql);
} catch (RuntimeException $e) {
// https://github.com/laminas/laminas-db/issues/56
$adapter->getDriver()->getConnection()->disconnect();
if (LaminasDbExceptionDetector::isConnectionException($e)) {
throw new ConnectionException($e->getMessage(), $e->getCode(), $e);
}
Expand Down Expand Up @@ -621,6 +637,8 @@ public function multiCreate($itemsData)
$multiInsertTableGw->getAdapter()->getDriver()->getConnection()->commit();
} catch (\Throwable $throwable) {
$multiInsertTableGw->getAdapter()->getDriver()->getConnection()->rollback();
// https://github.com/laminas/laminas-db/issues/56
$multiInsertTableGw->getAdapter()->getDriver()->getConnection()->disconnect();

throw new DataStoreException(
"Exception by multi create to table {$this->dbTable->table}. Details: {$throwable->getMessage()}",
Expand Down Expand Up @@ -703,6 +721,8 @@ private function beginTransaction(): void
try {
$this->dbTable->getAdapter()->getDriver()->getConnection()->beginTransaction();
} catch (RuntimeException $e) {
// https://github.com/laminas/laminas-db/issues/56
$this->dbTable->getAdapter()->getDriver()->getConnection()->disconnect();
if (LaminasDbExceptionDetector::isConnectionException($e)) {
throw new ConnectionException($e->getMessage(), $e->getCode(), $e);
}
Expand Down

0 comments on commit afedc33

Please sign in to comment.