|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Mssql\PDO; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Log\LogLevel; |
|
8
|
|
|
use Throwable; |
|
9
|
|
|
use Yiisoft\Db\AwareTrait\LoggerAwareTrait; |
|
10
|
|
|
use Yiisoft\Db\Connection\ConnectionPDOInterface; |
|
|
|
|
|
|
11
|
|
|
use Yiisoft\Db\Exception\Exception; |
|
12
|
|
|
use Yiisoft\Db\Exception\InvalidConfigException; |
|
13
|
|
|
use Yiisoft\Db\Exception\NotSupportedException; |
|
14
|
|
|
use Yiisoft\Db\Transaction\TransactionInterface; |
|
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
final class TransactionPDOMssql implements TransactionInterface |
|
17
|
|
|
{ |
|
18
|
|
|
use LoggerAwareTrait; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* A constant representing the transaction isolation level `READ UNCOMMITTED`. |
|
22
|
|
|
* |
|
23
|
|
|
* @link http://en.wikipedia.org/wiki/Isolation_%28database_systems%29#Isolation_levels} |
|
24
|
|
|
*/ |
|
25
|
|
|
public const READ_UNCOMMITTED = 'READ UNCOMMITTED'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* A constant representing the transaction isolation level `READ COMMITTED`. |
|
29
|
|
|
* |
|
30
|
|
|
* @link http://en.wikipedia.org/wiki/Isolation_%28database_systems%29#Isolation_levels} |
|
31
|
|
|
*/ |
|
32
|
|
|
public const READ_COMMITTED = 'READ COMMITTED'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* A constant representing the transaction isolation level `REPEATABLE READ`. |
|
36
|
|
|
* |
|
37
|
|
|
* @link http://en.wikipedia.org/wiki/Isolation_%28database_systems%29#Isolation_levels} |
|
38
|
|
|
*/ |
|
39
|
|
|
public const REPEATABLE_READ = 'REPEATABLE READ'; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* A constant representing the transaction isolation level `SERIALIZABLE`. |
|
43
|
|
|
* |
|
44
|
|
|
* {@see http://en.wikipedia.org/wiki/Isolation_%28database_systems%29#Isolation_levels} |
|
45
|
|
|
*/ |
|
46
|
|
|
public const SERIALIZABLE = 'SERIALIZABLE'; |
|
47
|
|
|
|
|
48
|
|
|
private int $level = 0; |
|
49
|
|
|
|
|
50
|
|
|
public function __construct(private ConnectionPDOInterface $db) |
|
51
|
|
|
{ |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function begin(?string $isolationLevel = null): void |
|
55
|
|
|
{ |
|
56
|
|
|
$this->db->open(); |
|
57
|
|
|
|
|
58
|
|
|
if ($this->level === 0) { |
|
59
|
|
|
if ($isolationLevel !== null) { |
|
60
|
|
|
$this->db->getSchema()->setTransactionIsolationLevel($isolationLevel); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$this->logger?->log( |
|
64
|
|
|
LogLevel::DEBUG, |
|
65
|
|
|
'Begin transaction' . ($isolationLevel ? ' with isolation level ' . $isolationLevel : '') |
|
66
|
|
|
. ' ' . __METHOD__ |
|
67
|
|
|
); |
|
68
|
|
|
|
|
69
|
|
|
$this->db->getPDO()?->beginTransaction(); |
|
70
|
|
|
$this->level = 1; |
|
71
|
|
|
|
|
72
|
|
|
return; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$schema = $this->db->getSchema(); |
|
76
|
|
|
|
|
77
|
|
|
if ($schema->supportsSavepoint()) { |
|
78
|
|
|
$this->logger?->log(LogLevel::DEBUG, 'Set savepoint ' . $this->level . ' ' . __METHOD__); |
|
79
|
|
|
$schema->createSavepoint('LEVEL' . $this->level); |
|
80
|
|
|
} else { |
|
81
|
|
|
$this->logger?->log( |
|
82
|
|
|
LogLevel::DEBUG, |
|
83
|
|
|
'Transaction not started: nested transaction not supported ' . __METHOD__ |
|
84
|
|
|
); |
|
85
|
|
|
throw new NotSupportedException('Transaction not started: nested transaction not supported.'); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$this->level++; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Commits a transaction. |
|
93
|
|
|
* |
|
94
|
|
|
* @throws Exception|Throwable if the transaction is not active |
|
95
|
|
|
*/ |
|
96
|
|
|
public function commit(): void |
|
97
|
|
|
{ |
|
98
|
|
|
if (!$this->isActive()) { |
|
99
|
|
|
throw new Exception('Failed to commit transaction: transaction was inactive.'); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$this->level--; |
|
103
|
|
|
|
|
104
|
|
|
if ($this->level === 0) { |
|
105
|
|
|
$this->logger?->log(LogLevel::DEBUG, 'Commit transaction ' . __METHOD__); |
|
106
|
|
|
$this->db->getPDO()?->commit(); |
|
107
|
|
|
return; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$schema = $this->db->getSchema(); |
|
111
|
|
|
|
|
112
|
|
|
if ($schema->supportsSavepoint()) { |
|
113
|
|
|
$this->logger?->log(LogLevel::DEBUG, 'Release savepoint ' . $this->level . ' ' . __METHOD__); |
|
114
|
|
|
$schema->releaseSavepoint('LEVEL' . $this->level); |
|
115
|
|
|
} else { |
|
116
|
|
|
$this->logger?->log( |
|
117
|
|
|
LogLevel::INFO, |
|
118
|
|
|
'Transaction not committed: nested transaction not supported ' . __METHOD__ |
|
119
|
|
|
); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function getLevel(): int |
|
124
|
|
|
{ |
|
125
|
|
|
return $this->level; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function isActive(): bool |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->level > 0 && $this->db->isActive(); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @throws Exception|InvalidConfigException|Throwable |
|
135
|
|
|
*/ |
|
136
|
|
|
public function rollBack(): void |
|
137
|
|
|
{ |
|
138
|
|
|
if (!$this->isActive()) { |
|
139
|
|
|
/** |
|
140
|
|
|
* do nothing if transaction is not active: this could be the transaction is committed but the event handler |
|
141
|
|
|
* to "commitTransaction" throw an exception |
|
142
|
|
|
*/ |
|
143
|
|
|
return; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$this->level--; |
|
147
|
|
|
if ($this->level === 0) { |
|
148
|
|
|
$this->logger?->log(LogLevel::INFO, 'Roll back transaction ' . __METHOD__); |
|
149
|
|
|
$this->db->getPDO()?->rollBack(); |
|
150
|
|
|
return; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
$schema = $this->db->getSchema(); |
|
154
|
|
|
if ($schema->supportsSavepoint()) { |
|
155
|
|
|
$this->logger?->log(LogLevel::DEBUG, 'Roll back to savepoint ' . $this->level . ' ' . __METHOD__); |
|
156
|
|
|
$schema->rollBackSavepoint('LEVEL' . $this->level); |
|
157
|
|
|
} else { |
|
158
|
|
|
$this->logger?->log( |
|
159
|
|
|
LogLevel::INFO, |
|
160
|
|
|
'Transaction not rolled back: nested transaction not supported ' . __METHOD__ |
|
161
|
|
|
); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
public function setIsolationLevel(string $level): void |
|
166
|
|
|
{ |
|
167
|
|
|
if (!$this->isActive()) { |
|
168
|
|
|
throw new Exception('Failed to set isolation level: transaction was inactive.'); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
$this->logger?->log( |
|
172
|
|
|
LogLevel::DEBUG, |
|
173
|
|
|
'Setting transaction isolation level to ' . $this->level . ' ' . __METHOD__ |
|
174
|
|
|
); |
|
175
|
|
|
$this->db->getSchema()->setTransactionIsolationLevel($level); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths