/
MySQLTransactionManager.php
91 lines (72 loc) · 2.29 KB
/
MySQLTransactionManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
namespace SilverStripe\ORM\Connect;
/**
* TransactionManager that executes MySQL-compatible transaction control queries
*/
class MySQLTransactionManager implements TransactionManager
{
protected $dbConn;
protected $inTransaction = false;
public function __construct(Database $dbConn)
{
$this->dbConn = $dbConn;
}
public function transactionStart($transactionMode = false, $sessionCharacteristics = false)
{
if ($this->inTransaction) {
throw new DatabaseException(
"Already in transaction, can't start another. Consider decorating with NestedTransactionManager."
);
}
// This sets the isolation level for the NEXT transaction, not the current one.
if ($transactionMode) {
$this->dbConn->query('SET TRANSACTION ' . $transactionMode);
}
$this->dbConn->query('START TRANSACTION');
if ($sessionCharacteristics) {
$this->dbConn->query('SET SESSION TRANSACTION ' . $sessionCharacteristics);
}
$this->inTransaction = true;
return true;
}
public function transactionEnd($chain = false)
{
if (!$this->inTransaction) {
throw new DatabaseException("Not in transaction, can't end.");
}
if ($chain) {
user_error(
"transactionEnd() chain argument no longer implemented. Use NestedTransactionManager",
E_USER_WARNING
);
}
$this->dbConn->query('COMMIT');
$this->inTransaction = false;
return true;
}
public function transactionRollback($savepoint = null)
{
if (!$this->inTransaction) {
throw new DatabaseException("Not in transaction, can't roll back.");
}
if ($savepoint) {
$this->dbConn->query("ROLLBACK TO SAVEPOINT $savepoint");
} else {
$this->dbConn->query('ROLLBACK');
$this->inTransaction = false;
}
return true;
}
public function transactionSavepoint($savepoint)
{
$this->dbConn->query("SAVEPOINT $savepoint");
}
public function transactionDepth()
{
return (int)$this->inTransaction;
}
public function supportsSavepoints()
{
return true;
}
}