Skip to content

Commit

Permalink
MDL-14992 pg session locking (8.2 and later only), refactoring and db…
Browse files Browse the repository at this point in the history
… session not default yet in new installs
  • Loading branch information
skodak committed Jan 17, 2009
1 parent a644e21 commit 5e9dd01
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 28 deletions.
20 changes: 7 additions & 13 deletions lib/dml/moodle_database.php
Expand Up @@ -77,7 +77,7 @@ abstract class moodle_database {
protected $last_type;
protected $last_extrainfo;

protected $used_for_db_sessions = 0;
protected $used_for_db_sessions = false;

/** internal temporary variable */
private $fix_sql_params_i;
Expand All @@ -98,13 +98,6 @@ public function __desctruct() {
$this->dispose();
}

/**
* Called only from session code!
*/
public function used_for_db_sessions() {
$this->used_for_db_sessions = 1;
}

/**
* Detects if all needed PHP stuff installed.
* Note: can be used before connect()
Expand Down Expand Up @@ -254,7 +247,7 @@ public function dispose() {
if ($this->used_for_db_sessions) {
// this is needed because we need to save session to db before closing it
session_write_close();
$this->used_for_db_sessions = 0;
$this->used_for_db_sessions = false;
}
if ($this->database_manager) {
$this->database_manager->dispose();
Expand Down Expand Up @@ -1740,14 +1733,15 @@ public function rollback_sql() {
}

/// session locking
public function session_lock_supported() {
return false;
}

public function get_session_lock($rowid) {
// TODO: make this abstract
return true;
$this->used_for_db_sessions = true;
}

public function release_session_lock($rowid) {
// TODO: make this abstract
return true;
}

/// performance and logging
Expand Down
19 changes: 8 additions & 11 deletions lib/dml/mysqli_native_moodle_database.php
Expand Up @@ -881,7 +881,12 @@ public function sql_regex($positivematch=true) {
}

/// session locking
public function session_lock_supported() {
return true;
}

public function get_session_lock($rowid) {
parent::get_session_lock($rowid);
$fullname = $this->dbname.'-'.$this->prefix.'-session-'.$rowid;
$sql = "SELECT GET_LOCK('$fullname',120)";
$this->query_start($sql, null, SQL_QUERY_AUX);
Expand All @@ -893,33 +898,25 @@ public function get_session_lock($rowid) {
$result->close();

if (reset($arr) == 1) {
return true;
return;
} else {
// try again!
return $this->get_session_lock($rowid);
$this->get_session_lock($rowid);
}
}

return false;
}

public function release_session_lock($rowid) {
parent::release_session_lock($rowid);
$fullname = $this->dbname.'-'.$this->prefix.'-session-'.$rowid;
$sql = "SELECT RELEASE_LOCK('$fullname')";
$this->query_start($sql, null, SQL_QUERY_AUX);
$result = $this->mysqli->query($sql);
$this->query_end($result);

if ($result) {
$arr = $result->fetch_assoc();
$result->close();

if (reset($arr) == 1) {
return true;
}
}

return false;
}

/// transactions
Expand Down
40 changes: 40 additions & 0 deletions lib/dml/pgsql_native_moodle_database.php
Expand Up @@ -1047,6 +1047,46 @@ public function sql_regex($positivematch=true) {
return $positivematch ? '~*' : '!~*';
}

/// session locking
public function session_lock_supported() {
return $this->is_min_version('8.2.0');
}

public function get_session_lock($rowid) {
// TODO: 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
if (!$this->session_lock_supported()) {
return;
}

parent::get_session_lock($rowid);
$sql = "SELECT pg_advisory_lock($rowid)";
$this->query_start($sql, null, SQL_QUERY_AUX);
$result = pg_query($this->pgsql, $sql);
$this->query_end($result);

if ($result) {
pg_free_result($result);
}
}

public function release_session_lock($rowid) {
if (!$this->session_lock_supported()) {
return;
}
parent::release_session_lock($rowid);

$sql = "SELECT pg_advisory_unlock($rowid)";
$this->query_start($sql, null, SQL_QUERY_AUX);
$result = pg_query($this->pgsql, $sql);
$this->query_end($result);

if ($result) {
pg_free_result($result);
}
}

/// transactions
/**
* on DBs that support it, switch to transaction mode and begin a transaction
Expand Down
7 changes: 3 additions & 4 deletions lib/sessionlib.php
Expand Up @@ -5,7 +5,7 @@
* @return moodle_session
*/
function session_get_instance() {
global $CFG;
global $CFG, $DB;

static $session = null;

Expand All @@ -18,7 +18,8 @@ function session_get_instance() {
$session_class = SESSION_CUSTOM;
$session = new $session_class();

} else if (!isset($CFG->dbsessions) or $CFG->dbsessions) {
//} else if ((!isset($CFG->dbsessions) or $CFG->dbsessions) and $DB->session_lock_supported()) {
} else if (!empty($CFG->dbsessions) and $DB->session_lock_supported()) {
// default recommended session type
$session = new database_session();

Expand Down Expand Up @@ -299,8 +300,6 @@ public function handler_open($save_path, $session_name) {
global $DB;

$this->database = $DB;
$this->database->used_for_db_sessions();

return true;
}

Expand Down

0 comments on commit 5e9dd01

Please sign in to comment.