MDL-51245 sqlsrv: Diagnose missing READ_COMMITTED_SNAPSHOT mode
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Fri, 4 Sep 2015 01:00:52 +0000 (03:00 +0200)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Tue, 8 Sep 2015 00:55:53 +0000 (02:55 +0200)
Without it, transactions are executed in standard READ_COMMITED
mode, without snapshoting/row versioning, leading to update
conflicts under high concurrency.

lib/dml/sqlsrv_native_moodle_database.php

index 36d7893..07d110b 100644 (file)
@@ -117,6 +117,37 @@ class sqlsrv_native_moodle_database extends moodle_database {
         return get_string('nativesqlsrvhelp', 'install');
     }
 
+    /**
+     * Diagnose database and tables, this function is used
+     * to verify database and driver settings, db engine types, etc.
+     *
+     * @return string null means everything ok, string means problem found.
+     */
+    public function diagnose() {
+        // Verify the database is running with READ_COMMITTED_SNAPSHOT enabled.
+        // (that's required to get snapshots/row versioning on READ_COMMITED mode).
+        $correctrcsmode = false;
+        $sql = "SELECT is_read_committed_snapshot_on
+                  FROM sys.databases
+                 WHERE name = '{$this->dbname}'";
+        $this->query_start($sql, null, SQL_QUERY_AUX);
+        $result = sqlsrv_query($this->sqlsrv, $sql);
+        $this->query_end($result);
+        if ($result) {
+            if ($row = sqlsrv_fetch_array($result)) {
+                $correctrcsmode = (bool)reset($row);
+            }
+        }
+        $this->free_result($result);
+
+        if (!$correctrcsmode) {
+            return get_string('mssqlrcsmodemissing', 'error');
+        }
+
+        // Arrived here, all right.
+        return null;
+    }
+
     /**
      * Connect to db
      * Must be called before most other methods. (you can call methods that return connection configuration parameters)