Repository navigation
Expand file tree
/
Copy pathDbalPersistedConnection.php
More file actions
155 lines (131 loc) · 3.4 KB
/
Copy pathDbalPersistedConnection.php
File metadata and controls
155 lines (131 loc) · 3.4 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
namespace Enqueue\JobQueue\Test;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\Connection as DriverConnection;
/**
* Connection wrapper sharing the same db handle across multiple requests.
*
* Allows multiple Connection instances to run in the same transaction
*/
class DbalPersistedConnection extends Connection
{
/**
* @var DriverConnection[]
*/
protected static $persistedConnections;
/**
* @var int[]
*/
protected static $persistedTransactionNestingLevels;
/**
* {@inheritdoc}
*/
public function connect()
{
if ($this->isConnected()) {
return false;
}
if ($this->hasPersistedConnection()) {
$this->_conn = $this->getPersistedConnection();
} else {
parent::connect();
$this->persistConnection($this->_conn);
}
return true;
}
/**
* {@inheritdoc}
*/
public function beginTransaction()
{
$this->wrapTransactionNestingLevel('beginTransaction');
}
/**
* {@inheritdoc}
*/
public function commit()
{
$this->wrapTransactionNestingLevel('commit');
}
/**
* {@inheritdoc}
*/
public function rollBack()
{
$this->wrapTransactionNestingLevel('rollBack');
}
/**
* @return int
*/
protected function getPersistedTransactionNestingLevel()
{
if (isset(static::$persistedTransactionNestingLevels[$this->getConnectionId()])) {
return static::$persistedTransactionNestingLevels[$this->getConnectionId()];
}
return 0;
}
/**
* @param int $level
*/
protected function persistTransactionNestingLevel($level)
{
static::$persistedTransactionNestingLevels[$this->getConnectionId()] = $level;
}
protected function persistConnection(DriverConnection $connection)
{
static::$persistedConnections[$this->getConnectionId()] = $connection;
}
/**
* @return bool
*/
protected function hasPersistedConnection()
{
return isset(static::$persistedConnections[$this->getConnectionId()]);
}
/**
* @return DriverConnection
*/
protected function getPersistedConnection()
{
return static::$persistedConnections[$this->getConnectionId()];
}
/**
* @return string
*/
protected function getConnectionId()
{
return md5(serialize($this->getParams()));
}
/**
* @param int $level
*/
private function setTransactionNestingLevel($level)
{
$rc = new \ReflectionClass(Connection::class);
$rp = $rc->hasProperty('transactionNestingLevel') ?
$rc->getProperty('transactionNestingLevel') :
$rc->getProperty('_transactionNestingLevel')
;
$rp->setAccessible(true);
$rp->setValue($this, $level);
$rp->setAccessible(false);
}
/**
* @param string $method
*
* @throws \Exception
*/
private function wrapTransactionNestingLevel($method)
{
$e = null;
$this->setTransactionNestingLevel($this->getPersistedTransactionNestingLevel());
try {
call_user_func(['parent', $method]);
} catch (\Exception $e) {
}
$this->persistTransactionNestingLevel($this->getTransactionNestingLevel());
if ($e) {
throw $e;
}
}
}