-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathDbManager.php
More file actions
168 lines (154 loc) · 4.83 KB
/
DbManager.php
File metadata and controls
168 lines (154 loc) · 4.83 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
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
declare (strict_types = 1);
namespace Webman\ThinkOrm;
use MongoDB\Driver\Command;
use Webman\Context;
use Workerman\Coroutine\Pool;
use Throwable;
use think\db\ConnectionInterface;
use think\db\BaseQuery;
use think\db\Query;
use support\Log;
/**
* Class DbManager.
*
* @mixin BaseQuery
* @mixin Query
*/
class DbManager extends \think\DbManager
{
/**
* Default heartbeat SQL (most SQL databases).
*
* @var string
*/
private const HEARTBEAT_SQL_DEFAULT = 'SELECT 1 AS ping';
/**
* Oracle heartbeat SQL.
*
* @var string
*/
private const HEARTBEAT_SQL_ORACLE = 'SELECT 1 AS ping FROM DUAL';
/**
* @var Pool[]
*/
protected static array $pools = [];
/**
* Get instance of connection.
*
* @param array|string|null $name
* @param bool $force
* @return ConnectionInterface
* @throws Throwable
*/
protected function instance(array|string|null $name = null, bool $force = false): ConnectionInterface
{
if (empty($name)) {
$name = $this->getConfig('default', 'mysql');
}
$key = "think-orm.connections.$name";
$connection = Context::get($key);
if (!$connection) {
if (!isset(static::$pools[$name])) {
$poolConfig = $this->config['connections'][$name]['pool'] ?? [];
$pool = new Pool($poolConfig['max_connections'] ?? 10, $poolConfig);
$pool->setConnectionCreator(function () use ($name) {
return $this->createConnection($name);
});
$pool->setConnectionCloser(function ($connection) use ($name) {
$this->closeConnection($connection, $name);
});
$pool->setHeartbeatChecker(function ($connection) {
$this->heartbeat($connection);
});
static::$pools[$name] = $pool;
}
try {
$connection = static::$pools[$name]->get();
Context::set($key, $connection);
} finally {
Context::onDestroy(function () use ($connection, $name) {
try {
if ($connection) {
if (method_exists($connection, 'getPdo')) {
$pdo = $connection->getPdo();
if ($pdo && $pdo->inTransaction()) {
$connection->rollBack();
Log::error("Uncommitted transaction found and try to rollback for connection: $name");
}
}
static::$pools[$name]->put($connection);
}
} catch (Throwable) {
// ignore
}
});
}
}
return $connection;
}
/**
* Heartbeat checker for pooled connections.
*
* @param mixed $connection
* @return void
*/
private function heartbeat(mixed $connection): void
{
$type = strtolower((string) $connection->getConfig('type'));
if ($type === 'mongo') {
$command = new Command(['ping' => 1]);
$connection->command($command);
return;
}
$connection->query($this->getHeartbeatSql($type));
}
/**
* Get heartbeat SQL.
*
* @param string $type
* @return string
*/
private function getHeartbeatSql(string $type): string
{
$type = strtolower($type);
return match ($type) {
'oracle',
'oci',
'oci8' => self::HEARTBEAT_SQL_ORACLE,
default => self::HEARTBEAT_SQL_DEFAULT,
};
}
/**
* Close connection.
*
* @param ConnectionInterface $connection
* @param string|null $name Connection config name used for Context cache key.
* @return void
*/
protected function closeConnection(ConnectionInterface $connection, ?string $name = null): void
{
if (method_exists($connection, 'getPdo')) {
try {
$pdo = $connection->getPdo();
if ($pdo && $pdo->inTransaction()) {
$connection->rollBack();
Log::error("Uncommitted transaction found and try to rollback for connection: $name");
}
} catch (Throwable) {
// ignore
}
}
$connection->close();
$clearProperties = function () {
$this->db = null;
$this->cache = null;
$this->builder = null;
};
$clearProperties->call($connection);
if ($name !== null) {
$key = "think-orm.connections.$name";
Context::set($key, null);
}
}
}