MDL-78324 core: Convert core/tag to esm
[moodle.git] / lib / dml / moodle_transaction.php
blob839f98d5cd3af6767cf245b467e421c80c5de313
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Delegated database transaction support.
20 * @package core_dml
21 * @copyright 2009 Petr Skoda (http://skodak.org)
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 /**
28 * Delegated transaction class.
30 * @package core_dml
31 * @copyright 2009 Petr Skoda (http://skodak.org)
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class moodle_transaction {
35 /** @var array The debug_backtrace() returned array.*/
36 private $start_backtrace;
37 /**@var moodle_database The moodle_database instance.*/
38 private $database = null;
40 /**
41 * Delegated transaction constructor,
42 * can be called only from moodle_database class.
43 * Unfortunately PHP's protected keyword is useless.
44 * @param moodle_database $database
46 public function __construct($database) {
47 $this->database = $database;
48 $this->start_backtrace = debug_backtrace();
49 array_shift($this->start_backtrace);
52 /**
53 * Returns backtrace of the code starting exception.
54 * @return array
56 public function get_backtrace() {
57 return $this->start_backtrace;
60 /**
61 * Is the delegated transaction already used?
62 * @return bool true if commit and rollback allowed, false if already done
64 public function is_disposed() {
65 return empty($this->database);
68 /**
69 * Mark transaction as disposed, no more
70 * commits and rollbacks allowed.
71 * To be used only from moodle_database class
72 * @return null
74 public function dispose() {
75 return $this->database = null;
78 /**
79 * Commit delegated transaction.
80 * The real database commit SQL is executed
81 * only after committing all delegated transactions.
83 * Incorrect order of nested commits or rollback
84 * at any level is resulting in rollback of SQL transaction.
86 * @return void
88 public function allow_commit() {
89 if ($this->is_disposed()) {
90 throw new dml_transaction_exception('Transactions already disposed', $this);
92 $this->database->commit_delegated_transaction($this);
95 /**
96 * Rollback all current delegated transactions.
98 * @param Exception|Throwable $e mandatory exception/throwable
99 * @return void
101 public function rollback($e) {
102 if ($this->is_disposed()) {
103 throw new dml_transaction_exception('Transactions already disposed', $this);
105 $this->database->rollback_delegated_transaction($this, $e);