Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
DatabaseTransactionsTrait.php
Go to the documentation of this file.
1<?php
2
3namespace Ubiquity\db\traits;
4
7
19 protected $transactionLevel = 0;
20
21 protected function nestable() {
22 return $this->wrapperObject->nestable ();
23 }
24
30 public function beginTransaction() {
31 if ($this->transactionLevel == 0 || ! $this->nestable ()) {
32 $ret = $this->wrapperObject->beginTransaction ();
33 Logger::info ( 'Transactions', 'Start transaction', 'beginTransaction' );
34 $this->transactionLevel ++;
35 return $ret;
36 }
37 $this->wrapperObject->savePoint ( $this->transactionLevel );
38 Logger::info ( 'Transactions', 'Savepoint level', 'beginTransaction', $this->transactionLevel );
39 $this->transactionLevel ++;
40 return true;
41 }
42
48 public function commit() {
49 $this->transactionLevel --;
50 if ($this->transactionLevel == 0 || ! $this->nestable ()) {
51 Logger::info ( 'Transactions', 'Commit transaction', 'commit' );
52 return $this->wrapperObject->commit ();
53 }
54 $this->wrapperObject->releasePoint ( $this->transactionLevel );
55 Logger::info ( 'Transactions', 'Release savepoint level', 'commit', $this->transactionLevel );
56 return true;
57 }
58
66 $res = true;
67 while ( $res && $this->transactionLevel > $transactionLevel ) {
68 $res = $this->commit ();
69 }
70 return $res;
71 }
72
78 public function commitAll() {
79 return $this->commitToLevel ( 0 );
80 }
81
87 public function rollBack() {
88 $this->transactionLevel --;
89 if ($this->transactionLevel == 0 || ! $this->nestable ()) {
90 Logger::info ( 'Transactions', 'Rollback transaction', 'rollBack' );
91 return $this->wrapperObject->rollBack ();
92 }
93 $this->wrapperObject->rollbackPoint ( $this->transactionLevel );
94 Logger::info ( 'Transactions', 'Rollback to savepoint level', 'rollBack', $this->transactionLevel );
95 return true;
96 }
97
105 $res = true;
106 while ( $res && $this->transactionLevel > $transactionLevel ) {
107 $res = $this->rollBack ();
108 }
109 return $res;
110 }
111
117 public function rollBackAll() {
118 return $this->rollBackToLevel ( 0 );
119 }
120
126 public function inTransaction() {
127 return $this->wrapperObject->inTransaction ();
128 }
129
138 public function callInTransaction($callback, ...$parameters) {
139 if ($this->beginTransaction ()) {
140 try {
141 $ret = call_user_func_array ( $callback, $parameters );
142 } catch ( \Exception $e ) {
143 $this->wrapperObject->rollBack ();
144 throw $e;
145 }
146
147 if ($ret) {
148 if (! $this->commit ()) {
149 throw new DBException ( 'Transaction was not committed.' );
150 }
151 } else {
152 $this->rollBack ();
153 }
154
155 return $ret;
156 }
157 throw new DBException ( 'Transaction was not started.' );
158 }
159
166 public function setIsolationLevel($isolationLevel) {
167 return $this->wrapperObject->setIsolationLevel($isolationLevel);
168 }
169}
setIsolationLevel($isolationLevel)
Sets the isolation level for transactions.
rollBackToLevel($transactionLevel)
Rolls back nested transactions up to level $transactionLevel.
commitAll()
Commits all nested transactions (up to level 0)
commitToLevel($transactionLevel)
Commits nested transactions up to level $transactionLevel.
rollBackAll()
Rolls back all nested transactions (up to level 0)
callInTransaction($callback,... $parameters)
Call a callback with an array of parameters in a transaction.
Abstract class for logging Ubiquity\log$Logger This class is part of Ubiquity.
Definition Logger.php:14