MDL-31532 restore: avoid null course->hiddensections problems. Credit goes to Petri...
[moodle.git] / lib / dml / moodle_transaction.php
blob33bfadbf85ca61ca009267b77c784e337a6a0b47
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 /**
20 * Delegated database transaction support.
22 * @package core
23 * @subpackage dml
24 * @copyright 2009 Petr Skoda (http://skodak.org)
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 defined('MOODLE_INTERNAL') || die();
30 /**
31 * Delegated transaction class.
33 class moodle_transaction {
34 private $start_backtrace;
35 private $database = null;
37 /**
38 * Delegated transaction constructor,
39 * can be called only from moodle_database class.
40 * Unfortunately PHP's protected keyword is useless.
41 * @param moodle_database $database
43 public function __construct($database) {
44 $this->database = $database;
45 $this->start_backtrace = debug_backtrace();
46 array_shift($this->start_backtrace);
49 /**
50 * Returns backtrace of the code starting exception.
51 * @return array
53 public function get_backtrace() {
54 return $this->start_backtrace;
57 /**
58 * Is the delegated transaction already used?
59 * @return bool true if commit and rollback allowed, false if already done
61 public function is_disposed() {
62 return empty($this->database);
65 /**
66 * Mark transaction as disposed, no more
67 * commits and rollbacks allowed.
68 * To be used only from moodle_database class
69 * @return unknown_type
71 public function dispose() {
72 return $this->database = null;
75 /**
76 * Commit delegated transaction.
77 * The real database commit SQL is executed
78 * only after committing all delegated transactions.
80 * Incorrect order of nested commits or rollback
81 * at any level is resulting in rollback of SQL transaction.
83 * @return void
85 public function allow_commit() {
86 if ($this->is_disposed()) {
87 throw new dml_transaction_exception('Transactions already disposed', $this);
89 $this->database->commit_delegated_transaction($this);
92 /**
93 * Rollback all current delegated transactions.
95 * @param Exception $e mandatory exception
96 * @return void
98 public function rollback(Exception $e) {
99 if ($this->is_disposed()) {
100 throw new dml_transaction_exception('Transactions already disposed', $this);
102 $this->database->rollback_delegated_transaction($this, $e);