more comment typos
[moodle.git] / lib / dml / moodle_transaction.php
CommitLineData
d5a8d9aa
PS
1<?php
2
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.
14//
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/>.
17
18
19/**
20 * Delegated database transaction support.
21 *
22 * @package moodlecore
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
26 */
27
28/**
29 * Delegated transaction class.
30 */
31class moodle_transaction {
32 private $start_backtrace;
33 private $database = null;
34
35 /**
36 * Delegated transaction constructor,
37 * can be called only from moodle_database class.
38 * Unfortunately PHP's protected keyword is useless.
39 * @param moodle_database $database
40 */
41 public function __construct($database) {
42 $this->database = $database;
43 $this->start_backtrace = debug_backtrace();
07b81ebd
PS
44 array_shift($this->start_backtrace);
45 }
46
47 /**
48 * Returns backtrace of the code starting exception.
49 * @return array
50 */
51 public function get_backtrace() {
52 return $this->start_backtrace;
d5a8d9aa
PS
53 }
54
55 /**
56 * Is the delegated transaction already used?
57 * @return bool true if commit and rollback allowed, false if already done
58 */
59 public function is_disposed() {
60 return empty($this->database);
61 }
62
63 /**
64 * Mark transaction as disposed, no more
65 * commits and rollbacks allowed.
66 * To be used only from moodle_database class
67 * @return unknown_type
68 */
69 public function dispose() {
70 return $this->database = null;
71 }
72
73 /**
74 * Commit delegated transaction.
75 * The real database commit SQL is executed
4d276e34 76 * only after committing all delegated transactions.
d5a8d9aa
PS
77 *
78 * Incorrect order of nested commits or rollback
79 * at any level is resulting in rollback of SQL transaction.
80 *
81 * @return void
82 */
83 public function allow_commit() {
84 if ($this->is_disposed()) {
85 throw new dml_transaction_exception('Transactions already disposed', $this);
86 }
87 $this->database->commit_delegated_transaction($this);
88 }
89
90 /**
91 * Rollback all current delegated transactions.
92 *
93 * @param Exception $e mandatory exception
94 * @return void
95 */
96 public function rollback(Exception $e) {
97 if ($this->is_disposed()) {
98 throw new dml_transaction_exception('Transactions already disposed', $this);
99 }
100 $this->database->rollback_delegated_transaction($this, $e);
101 }
102}