diff --git a/front/deviceDetails.php b/front/deviceDetails.php
index a3a3947e..6ba2de3f 100755
--- a/front/deviceDetails.php
+++ b/front/deviceDetails.php
@@ -500,14 +500,10 @@
- = lang('DevDetail_button_DeleteEvents');?>
- = lang('DevDetail_button_Delete');?>
- = lang('DevDetail_button_Reset');?>
- = lang('DevDetail_button_Save');?>
+ = lang('DevDetail_button_DeleteEvents');?>
+ = lang('DevDetail_button_Delete');?>
+
+ = lang('DevDetail_button_Save');?>
diff --git a/front/maintenance.php b/front/maintenance.php
index 0a87da8f..e041c800 100755
--- a/front/maintenance.php
+++ b/front/maintenance.php
@@ -467,6 +467,7 @@ $db->close();
diff --git a/front/php/server/db.php b/front/php/server/db.php
index 8ed0f736..e13035f3 100755
--- a/front/php/server/db.php
+++ b/front/php/server/db.php
@@ -58,37 +58,83 @@ function SQLite3_connect ($trytoreconnect, $retryCount = 0) {
}
+//------------------------------------------------------------------------------
+// ->query override to handle retries
+//------------------------------------------------------------------------------
+class CustomDatabaseWrapper {
+ private $sqlite;
+ private $maxRetries;
+ private $retryDelay;
+
+
+ public function __construct($filename, $flags = SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE, $maxRetries = 3, $retryDelay = 1000, $encryptionKey = null) {
+ $this->sqlite = new SQLite3($filename, $flags, $encryptionKey);
+
+ $this->maxRetries = $maxRetries;
+ $this->retryDelay = $retryDelay;
+ }
+
+ public function query(string $query): SQLite3Result|bool {
+ global $DBFILE_LOCKED_FILE;
+
+ $attempts = 0;
+ while ($attempts < $this->maxRetries) {
+ $result = $this->sqlite->query($query);
+ if ($result !== false) {
+ // Write unlock status to the locked file
+ file_put_contents($DBFILE_LOCKED_FILE, '0');
+ return $result;
+ }
+
+ // Write lock status to the locked file
+ file_put_contents($DBFILE_LOCKED_FILE, '1');
+
+ $attempts++;
+ usleep($this->retryDelay * 1000); // Retry delay in milliseconds
+ }
+
+ // If all retries failed, throw an exception or handle the error as needed
+ echo '';
+ throw new Exception("Query failed after {$this->maxRetries} attempts: " . $this->sqlite->lastErrorMsg());
+ }
+
+ // Delegate other SQLite3 methods to the $sqlite instance
+ public function __call($name, $arguments) {
+ return call_user_func_array([$this->sqlite, $name], $arguments);
+ }
+}
+
+
//------------------------------------------------------------------------------
// Open DB
//------------------------------------------------------------------------------
-function OpenDB (...$DBPath) {
+function OpenDB($DBPath = null) {
global $DBFILE;
global $db;
- // use custom path if supplied
- foreach ($DBPath as $path) {
- $DBFILE = $path;
- }
-
- if(strlen($DBFILE) == 0)
- {
- echo '';
- die ('Database not available
');
+ // Use custom path if supplied
+ if ($DBPath !== null) {
+ $DBFILE = $DBPath;
}
- $db = SQLite3_connect(true);
-
- if(!$db)
- {
- echo '';
- die ('Error connecting to the database
');
+ if (strlen($DBFILE) == 0) {
+ echo '';
+ die('Database not available
');
+ }
+
+ try {
+ $db = new CustomDatabaseWrapper($DBFILE);
+ } catch (Exception $e) {
+ echo '';
+ die('Error connecting to the database
');
}
$db->exec('PRAGMA journal_mode = wal;');
}
+
// # Open DB once and keep open
// # Opening / closing DB frequently actually casues more issues
OpenDB (); // main