Skip to content

Commit

Permalink
Reset transaction settings to default on transaction end (Fixes #324)
Browse files Browse the repository at this point in the history
  • Loading branch information
mstilkerich committed Nov 29, 2020
1 parent 6f08ad0 commit 0c6a7bf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Fix #321: Boolean settings in presets caused errors when trying to store the preset's addressbooks to the database
- Fix #322: The refresh time string from admin presets was not converted to seconds, causing errors or wrong values when
storing the preset's addressbooks to the database
- Fix #324: Changes not immediately visible with postgresql (delete contact, add/remove contact to/from group)

## Version 4.0.3 (to 4.0.2)

Expand Down
27 changes: 27 additions & 0 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ public static function endTransaction(): void
self::$logger->error("Database::endTransaction ERROR: " . $dbh->is_error());
throw new DatabaseException($dbh->is_error());
}

$this->resetTransactionSettings();
} else {
throw new \Exception("Attempt to commit a transaction while not within a transaction");
}
Expand All @@ -142,6 +144,8 @@ public static function rollbackTransaction(): void
self::$logger->error("Database::rollbackTransaction ERROR: " . $dbh->is_error());
throw new \Exception($dbh->is_error());
}

$this->resetTransactionSettings();
} else {
// not throwing an error here facilitates usage of the interface at caller side. The caller
// can issue rollback without having to keep track whether an error occurred before/after a
Expand All @@ -150,6 +154,29 @@ public static function rollbackTransaction(): void
}
}

/**
* Resets database transaction settings to defaults that should apply to autocommit transactions.
*/
private function resetTransactionSettings(): void
{
$logger = $this->logger;
$dbh = $this->dbHandle;
switch ($dbh->db_provider) {
case "postgres":
$ret = $dbh->query(
"SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL REPEATABLE READ, READ WRITE"
);
break;
default:
return;
}

// reset default session characteristics to read/write for autocommit single-statement transactions
if ($ret === false) {
$logger->warning(__METHOD__ . " ERROR: " . $dbh->is_error());
}
}

/**
* Checks if the database schema is up to date and performs migrations if needed.
*
Expand Down

0 comments on commit 0c6a7bf

Please sign in to comment.