]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-19470 detection of nested transactions, only one level allowed
authorskodak <skodak>
Fri, 12 Jun 2009 07:55:44 +0000 (07:55 +0000)
committerskodak <skodak>
Fri, 12 Jun 2009 07:55:44 +0000 (07:55 +0000)
lib/dml/adodb_moodle_database.php
lib/dml/moodle_database.php
lib/dml/mysqli_native_moodle_database.php
lib/dml/oci_native_moodle_database.php
lib/dml/pdo_moodle_database.php
lib/dml/pgsql_native_moodle_database.php

index 9a033bf4942a872f455c1291be797851955f8ccf..e32444c0570434c2f9e09a349e99c1ba200c6abe 100644 (file)
@@ -508,14 +508,23 @@ abstract class adodb_moodle_database extends moodle_database {
     }
 
     public function begin_sql() {
+        if (!parent::begin_sql()) {
+            return false;
+        }
         $this->adodb->BeginTrans();
         return true;
     }
     public function commit_sql() {
+        if (!parent::commit_sql()) {
+            return false;
+        }
         $this->adodb->CommitTrans();
         return true;
     }
     public function rollback_sql() {
+        if (!parent::rollback_sql()) {
+            return false;
+        }
         $this->adodb->RollbackTrans();
         return true;
     }
index e8070348ff04240b483d165984d52c4e0bfa21bc..d773531061aa9f5c6a6e350ca27363e7ff73354b 100644 (file)
@@ -79,6 +79,9 @@ abstract class moodle_database {
 
     protected $used_for_db_sessions = false;
 
+    /** Flag indicating transaction in progress */
+    protected $intransaction = false;
+
     /** internal temporary variable */
     private $fix_sql_params_i;
 
@@ -248,6 +251,9 @@ abstract class moodle_database {
      * Do NOT use connect() again, create a new instance if needed.
      */
     public function dispose() {
+        if ($this->intransaction) {
+            error_log('Active database transaction detected when disposing database!'); // probably can not write to console anymore, log problem instead  
+        }
         if ($this->used_for_db_sessions) {
             // this is needed because we need to save session to db before closing it
             session_get_instance()->write_close();
@@ -1739,23 +1745,46 @@ abstract class moodle_database {
      * you'll need to ensure you call commit_sql() or your changes *will* be lost.
      *
      * this is _very_ useful for massive updates
+     *
+     * Please note only one level of transactions is supported, please do not use
+     * transaction in moodle core! Transaction are intended for web services
+     * enrolment and auth synchronisation scripts, etc.
+     *
+     * @return bool success
      */
     public function begin_sql() {
-        return false;
+        if ($this->intransaction) {
+            debugging('Transaction already in progress');
+            return false;
+        }
+        $this->intransaction = true;
+        return true;
     }
 
     /**
      * on DBs that support it, commit the transaction
+     * @return bool success
      */
     public function commit_sql() {
-        return false;
+        if (!$this->intransaction) {
+            debugging('Transaction not in progress');
+            return false;
+        }
+        $this->intransaction = false;
+        return true;
     }
 
     /**
      * on DBs that support it, rollback the transaction
+     * @return bool success
      */
     public function rollback_sql() {
-        return false;
+        if (!$this->intransaction) {
+            debugging('Transaction not in progress');
+            return false;
+        }
+        $this->intransaction = false;
+        return true;
     }
 
 /// session locking
index 12871c106f907e8b58e07066e3112b6d6aaee38d..ef5d08b74eb62f063c62652cc2321ba1b8b870be 100644 (file)
@@ -956,6 +956,9 @@ class mysqli_native_moodle_database extends moodle_database {
      * this is _very_ useful for massive updates
      */
     public function begin_sql() {
+        if (!parent::begin_sql()) {
+            return false;
+        }
         $sql = "SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED";
         $this->query_start($sql, NULL, SQL_QUERY_AUX);
         $result = $this->mysqli->query($sql);
@@ -973,6 +976,9 @@ class mysqli_native_moodle_database extends moodle_database {
      * on DBs that support it, commit the transaction
      */
     public function commit_sql() {
+        if (!parent::commit_sql()) {
+            return false;
+        }
         $sql = "COMMIT";
         $this->query_start($sql, NULL, SQL_QUERY_AUX);
         $result = $this->mysqli->query($sql);
@@ -985,6 +991,9 @@ class mysqli_native_moodle_database extends moodle_database {
      * on DBs that support it, rollback the transaction
      */
     public function rollback_sql() {
+        if (!parent::rollback_sql()) {
+            return false;
+        }
         $sql = "ROLLBACK";
         $this->query_start($sql, NULL, SQL_QUERY_AUX);
         $result = $this->mysqli->query($sql);
index 04a2aa630144a28a6fbd43894e9f001d4a4cd5d5..9a586cd6d6af48639f6832cd2464a81121dd7152 100644 (file)
@@ -1215,7 +1215,10 @@ class oci_native_moodle_database extends moodle_database {
      * this is _very_ useful for massive updates
      */
     public function begin_sql() {
-        return false;
+        if (!parent::begin_sql()) {
+            return false;
+        }
+        return true;
 
         $sql = "BEGIN";
         $this->query_start($sql, NULL, SQL_QUERY_AUX);
@@ -1230,7 +1233,10 @@ class oci_native_moodle_database extends moodle_database {
      * on DBs that support it, commit the transaction
      */
     public function commit_sql() {
-        return false;
+        if (!parent::commit_sql()) {
+            return false;
+        }
+        return true;
 
         $sql = "COMMIT";
         $this->query_start($sql, NULL, SQL_QUERY_AUX);
@@ -1245,7 +1251,10 @@ class oci_native_moodle_database extends moodle_database {
      * on DBs that support it, rollback the transaction
      */
     public function rollback_sql() {
-        return false;
+        if (!parent::rollback_sql()) {
+            return false;
+        }
+        return true;
 
         $sql = "ROLLBACK";
         $this->query_start($sql, NULL, SQL_QUERY_AUX);
index 294174696e0e271718c57a488d279aec59a6b046..b88cebd947970b3a0cee8e97e67dae6f4d3d2852 100644 (file)
@@ -525,6 +525,9 @@ abstract class pdo_moodle_database extends moodle_database {
     }
 
     public function begin_sql() {
+        if (!parent::begin_sql()) {
+            return false;
+        }
         try {
             $this->pdb->beginTransaction();
             return true;
@@ -533,6 +536,9 @@ abstract class pdo_moodle_database extends moodle_database {
         }
     }
     public function commit_sql() {
+        if (!parent::commit_sql()) {
+            return false;
+        }
         try {
             $this->pdb->commit();
             return true;
@@ -542,6 +548,9 @@ abstract class pdo_moodle_database extends moodle_database {
     }
 
     public function rollback_sql() {
+        if (!parent::rollback_sql()) {
+            return false;
+        }
         try {
             $this->pdb->rollBack();
             return true;
index a2999a873dbb4dbed112bde5ba3121d2ae3d6a10..288f44b4eb2b51069d03bdd896a14590eb5875c7 100644 (file)
@@ -1107,6 +1107,9 @@ class pgsql_native_moodle_database extends moodle_database {
      * this is _very_ useful for massive updates
      */
     public function begin_sql() {
+        if (!parent::begin_sql()) {
+            return false;
+        }
         $sql = "BEGIN ISOLATION LEVEL READ COMMITTED";
         $this->query_start($sql, NULL, SQL_QUERY_AUX);
         $result = pg_query($this->pgsql, $sql);
@@ -1120,6 +1123,9 @@ class pgsql_native_moodle_database extends moodle_database {
      * on DBs that support it, commit the transaction
      */
     public function commit_sql() {
+        if (!parent::commit_sql()) {
+            return false;
+        }
         $sql = "COMMIT";
         $this->query_start($sql, NULL, SQL_QUERY_AUX);
         $result = pg_query($this->pgsql, $sql);
@@ -1133,6 +1139,9 @@ class pgsql_native_moodle_database extends moodle_database {
      * on DBs that support it, rollback the transaction
      */
     public function rollback_sql() {
+        if (!parent::rollback_sql()) {
+            return false;
+        }
         $sql = "ROLLBACK";
         $this->query_start($sql, NULL, SQL_QUERY_AUX);
         $result = pg_query($this->pgsql, $sql);