Skip to content

Commit a418c77

Browse files
committed
未提交事务检测兼容webman2.1
1 parent fb016cd commit a418c77

1 file changed

Lines changed: 19 additions & 38 deletions

File tree

src/Middleware.php

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Events\Dispatcher;
99
use Illuminate\Redis\Events\CommandExecuted;
1010
use support\Context;
11+
use think\db\PDOConnection;
1112
use Webman\Http\Request;
1213
use Webman\Http\Response;
1314
use Webman\MiddlewareInterface;
@@ -217,16 +218,18 @@ protected function initDbListen()
217218
*/
218219
protected function tryInitRedisListen(): array
219220
{
220-
static $listened_names = [];
221+
static $listened;
221222
if (!class_exists(CommandExecuted::class)) {
222223
return [];
223224
}
224225
$new_names = [];
226+
$listened ??= new \WeakMap();
227+
// Cache 目前无法监听 日志
225228
try {
226229
foreach (Redis::instance()->connections() ?: [] as $connection) {
227230
/* @var \Illuminate\Redis\Connections\Connection $connection */
228231
$name = $connection->getName();
229-
if (isset($listened_names[$name])) {
232+
if (isset($listened[$connection])) {
230233
continue;
231234
}
232235
$connection->listen(function (CommandExecuted $command) {
@@ -237,7 +240,7 @@ protected function tryInitRedisListen(): array
237240
}
238241
Context::get()->webmanLogs = (Context::get()->webmanLogs ?? '') . "[Redis]\t[connection:{$command->connectionName}] Redis::{$command->command}('" . implode('\', \'', $command->parameters) . "') ({$command->time} ms)" . PHP_EOL;
239242
});
240-
$listened_names[$name] = $name;
243+
$listened[$connection] = $name;
241244
$new_names[] = $name;
242245
}
243246
} catch (Throwable $e) {
@@ -250,7 +253,7 @@ protected function tryInitRedisListen(): array
250253
/**
251254
* 获得Db的Manager
252255
*
253-
* @return mixed
256+
* @return \Webman\Database\Manager
254257
*/
255258
protected function getCapsule()
256259
{
@@ -272,19 +275,14 @@ protected function getCapsule()
272275
protected function checkDbUncommittedTransaction(): string
273276
{
274277
$logs = '';
275-
try {
276-
foreach ($this->getCapsule()->getDatabaseManager()->getConnections() as $connection) {
277-
/* @var \Illuminate\Database\MySqlConnection $connection * */
278-
if (\in_array($connection->getConfig('driver'), ['mysql', 'pgsql', 'sqlite', 'sqlsrv'])) {
279-
$pdo = $connection->getPdo();
280-
if ($pdo && $pdo->inTransaction()) {
281-
$connection->rollBack();
282-
$logs .= "[ERROR]\tUncommitted transaction found and try to rollback" . PHP_EOL;
283-
}
278+
$context = Context::get();
279+
foreach ($context as $item) {
280+
if ($item instanceof Connection) {
281+
if ($item->transactionLevel() > 0) {
282+
$item->rollBack();
283+
$logs .= "[ERROR]\tUncommitted transaction found and try to rollback" . PHP_EOL;
284284
}
285285
}
286-
} catch (Throwable $e) {
287-
echo $e;
288286
}
289287
return $logs;
290288
}
@@ -298,34 +296,17 @@ protected function checkTpUncommittedTransaction(): string
298296
{
299297
static $property, $manager_instance;
300298
$logs = '';
301-
try {
302-
if (!$property) {
303-
if (class_exists(ThinkContainer::class, false)) {
304-
$manager_instance = ThinkContainer::getInstance()->make(DbManager::class);
305-
} else {
306-
$reflect = new \ReflectionClass(ThinkDb::class);
307-
$property = $reflect->getProperty('instance');
308-
$property->setAccessible(true);
309-
$manager_instance = $property->getValue();
310-
}
311-
$reflect = new \ReflectionClass($manager_instance);
312-
$property = $reflect->getProperty('instance');
313-
$property->setAccessible(true);
314-
}
315-
316-
$instances = $property->getValue($manager_instance);
317-
foreach ($instances as $connection) {
318-
/* @var \think\db\connector\Mysql $connection */
319-
if (method_exists($connection, 'getPdo')) {
320-
$pdo = $connection->getPdo();
299+
$context = Context::get();
300+
foreach ($context as $item) {
301+
if ($item instanceof PDOConnection) {
302+
if (method_exists($item, 'getPdo')) {
303+
$pdo = $item->getPdo();
321304
if ($pdo && $pdo->inTransaction()) {
322-
$connection->rollBack();
305+
$item->rollBack();
323306
$logs .= "[ERROR]\tUncommitted transaction found and try to rollback" . PHP_EOL;
324307
}
325308
}
326309
}
327-
} catch (Throwable $e) {
328-
echo $e;
329310
}
330311
return $logs;
331312
}

0 commit comments

Comments
 (0)