MDL-30026 improve session lock acquire timeouts and other minor cleanup

This is partially based on original patch by Tony Levi.
This commit is contained in:
Petr Skoda
2012-04-12 10:27:28 +02:00
parent 8e35d0aef8
commit a6ecee4ca0
9 changed files with 329 additions and 62 deletions
+39 -2
View File
@@ -1153,7 +1153,13 @@ class pgsql_native_moodle_database extends moodle_database {
return true;
}
public function get_session_lock($rowid) {
/**
* Obtain session lock
* @param int $rowid id of the row with session record
* @param int $timeout max allowed time to wait for the lock in seconds
* @return bool success
*/
public function get_session_lock($rowid, $timeout) {
// NOTE: there is a potential locking problem for database running
// multiple instances of moodle, we could try to use pg_advisory_lock(int, int),
// luckily there is not a big chance that they would collide
@@ -1161,9 +1167,40 @@ class pgsql_native_moodle_database extends moodle_database {
return;
}
parent::get_session_lock($rowid);
parent::get_session_lock($rowid, $timeout);
$timeoutmilli = $timeout * 1000;
$sql = "SET statement_timeout TO $timeoutmilli";
$this->query_start($sql, null, SQL_QUERY_AUX);
$result = pg_query($this->pgsql, $sql);
$this->query_end($result);
if ($result) {
pg_free_result($result);
}
$sql = "SELECT pg_advisory_lock($rowid)";
$this->query_start($sql, null, SQL_QUERY_AUX);
$start = time();
$result = pg_query($this->pgsql, $sql);
$end = time();
try {
$this->query_end($result);
} catch (dml_exception $ex) {
if ($end - $start >= $timeout) {
throw new dml_sessionwait_exception();
} else {
throw $ex;
}
}
if ($result) {
pg_free_result($result);
}
$sql = "SET statement_timeout TO DEFAULT";
$this->query_start($sql, null, SQL_QUERY_AUX);
$result = pg_query($this->pgsql, $sql);
$this->query_end($result);