1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Core; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
6
|
|
|
use eZ\Publish\API\Repository\Repository; |
7
|
|
|
use ProxyManager\Proxy\ValueHolderInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Functionality related to managing transactions - both "repo transactions" and "db transactions" |
11
|
|
|
* |
12
|
|
|
* Implemented as a trait to avoid breaking BC as much as possible. |
13
|
|
|
* @todo transform this into an interface + a service, when bumping a major version |
14
|
|
|
*/ |
15
|
|
|
trait TransactionManagerTrait |
16
|
|
|
{ |
17
|
|
|
/** @var Repository $repository */ |
18
|
|
|
protected $repository; |
19
|
|
|
|
20
|
|
|
/** @var Connection $connection */ |
21
|
|
|
protected $connection; |
22
|
|
|
|
23
|
|
|
public function setRepository(Repository $repository): void |
24
|
|
|
{ |
25
|
|
|
// NB: ideally we should retrieve the DB connection from the Repository. But that means going through |
26
|
|
|
// multiple steps of access to protected/private members (persistenceHandler / transactionHandler / ...) which |
27
|
|
|
// are not guaranteed to be stable, or even present. So we inject th connection separately... |
28
|
|
|
|
29
|
|
|
$this->repository = $repository; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function setConnection(Connection $connection): void |
33
|
|
|
{ |
34
|
|
|
$this->connection = $connection; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Repo transaction |
39
|
|
|
* @return void |
40
|
|
|
*/ |
41
|
|
|
protected function beginTransaction(): void |
42
|
|
|
{ |
43
|
|
|
$this->repository->beginTransaction(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Repo transaction |
48
|
|
|
* @return void |
49
|
|
|
*/ |
50
|
|
|
protected function commit(): void |
51
|
|
|
{ |
52
|
|
|
$this->repository->commit(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Repo transaction |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
|
protected function rollback(): void |
60
|
|
|
{ |
61
|
|
|
$this->repository->rollback(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Resets the transaction counter and all other transaction-related info for the current db connection. |
66
|
|
|
* To be used only when we know for sure the db has no active transactions, and the DBAL object is out of sync |
67
|
|
|
* @internal |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
|
|
protected function resetDBTransaction() |
71
|
|
|
{ |
72
|
|
|
/** @var Connection $connection */ |
73
|
|
|
$connection = ($this->connection instanceof ValueHolderInterface) ? $this->connection->getWrappedValueHolderValue() : $this->connection; |
74
|
|
|
$cl = \Closure::bind(function () { |
75
|
|
|
$this->transactionNestingLevel = 0; |
|
|
|
|
76
|
|
|
$this->isRollbackOnly = false; |
|
|
|
|
77
|
|
|
}, |
78
|
|
|
$connection, |
79
|
|
|
$connection |
80
|
|
|
); |
81
|
|
|
$cl(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Returns the current db transaction nesting level. |
86
|
|
|
* |
87
|
|
|
* @return int The nesting level. A value of 0 means there's no active transaction. |
88
|
|
|
*/ |
89
|
|
|
public function getDBTransactionNestingLevel() |
90
|
|
|
{ |
91
|
|
|
return $this->connection->getTransactionNestingLevel(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function rollbackDBTransaction() |
95
|
|
|
{ |
96
|
|
|
return $this->connection->rollBack(); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|