Files
tougao/application/common/DbReconnectHelper.php
wyn 8785610e6d 参考文献作者堆叠
参考文献相关性检测
作者ai写作辅助检测工作
2026-07-15 10:49:05 +08:00

56 lines
1.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace app\common;
use think\Db;
/**
* 长耗时任务HTTP 拉摘要、LLM前后释放/重建 MySQL 连接,避免 Windows errno=10053
*/
class DbReconnectHelper
{
public static function release()
{
try {
Db::close();
} catch (\Throwable $e) {
// ignore
}
}
/**
* 确保当前进程可用数据库连接(断线则强制重连)
*/
public static function ensure()
{
if (self::ping()) {
return true;
}
self::release();
return self::reconnect();
}
private static function ping()
{
try {
Db::query('SELECT 1');
return true;
} catch (\Throwable $e) {
return false;
}
}
private static function reconnect()
{
try {
Db::connect(config('database'), true);
Db::query('SELECT 1');
return true;
} catch (\Throwable $e) {
return false;
}
}
}