Merge branch 'MDL-52284-master' of git://github.com/marinaglancy/moodle
[moodle.git] / lib / dml / moodle_transaction.php
CommitLineData
d5a8d9aa 1<?php
d5a8d9aa
PS
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.
13//
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/>.
16
d5a8d9aa
PS
17/**
18 * Delegated database transaction support.
19 *
00902cd9 20 * @package core_dml
d5a8d9aa
PS
21 * @copyright 2009 Petr Skoda (http://skodak.org)
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
f2ed3f05
PS
25defined('MOODLE_INTERNAL') || die();
26
d5a8d9aa
PS
27/**
28 * Delegated transaction class.
6df26010 29 *
00902cd9 30 * @package core_dml
6df26010
AB
31 * @copyright 2009 Petr Skoda (http://skodak.org)
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
d5a8d9aa
PS
33 */
34class moodle_transaction {
6df26010 35 /** @var array The debug_backtrace() returned array.*/
d5a8d9aa 36 private $start_backtrace;
6df26010 37 /**@var moodle_database The moodle_database instance.*/
d5a8d9aa
PS
38 private $database = null;
39
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
45 */
46 public function __construct($database) {
47 $this->database = $database;
48 $this->start_backtrace = debug_backtrace();
07b81ebd
PS
49 array_shift($this->start_backtrace);
50 }
51
52 /**
53 * Returns backtrace of the code starting exception.
54 * @return array
55 */
56 public function get_backtrace() {
57 return $this->start_backtrace;
d5a8d9aa
PS
58 }
59
60 /**
61 * Is the delegated transaction already used?
62 * @return bool true if commit and rollback allowed, false if already done
63 */
64 public function is_disposed() {
65 return empty($this->database);
66 }
67
68 /**
69 * Mark transaction as disposed, no more
70 * commits and rollbacks allowed.
71 * To be used only from moodle_database class
6df26010 72 * @return null
d5a8d9aa
PS
73 */
74 public function dispose() {
75 return $this->database = null;
76 }
77
78 /**
79 * Commit delegated transaction.
80 * The real database commit SQL is executed
4d276e34 81 * only after committing all delegated transactions.
d5a8d9aa
PS
82 *
83 * Incorrect order of nested commits or rollback
84 * at any level is resulting in rollback of SQL transaction.
85 *
86 * @return void
87 */
88 public function allow_commit() {
89 if ($this->is_disposed()) {
90 throw new dml_transaction_exception('Transactions already disposed', $this);
91 }
92 $this->database->commit_delegated_transaction($this);
93 }
94
95 /**
96 * Rollback all current delegated transactions.
97 *
1766e6a1 98 * @param Exception|Throwable $e mandatory exception/throwable
d5a8d9aa
PS
99 * @return void
100 */
d74b7e42 101 public function rollback($e) {
d5a8d9aa
PS
102 if ($this->is_disposed()) {
103 throw new dml_transaction_exception('Transactions already disposed', $this);
104 }
105 $this->database->rollback_delegated_transaction($this, $e);
106 }
00902cd9 107}