abstraction for DBPool reset function, update to 3.1.14

This commit is contained in:
crazywhalecc 2023-05-30 22:19:52 +08:00 committed by Jerry Ma
parent 98ac59592b
commit 0b8409962c
4 changed files with 46 additions and 26 deletions

View File

@ -262,30 +262,7 @@ class WorkerEventListener
*/
private function initConnectionPool(): void
{
// 清空 MySQL 的连接池
foreach (DBPool::getAllPools() as $name => $pool) {
DBPool::destroyPool($name);
}
// 清空 Redis 连接池
foreach (RedisPool::getAllPools() as $name => $pool) {
RedisPool::destroyPool($name);
}
// 读取 MySQL/PostgresSQL/SQLite 配置文件并创建连接池
$conf = config('global.database');
// 如果有多个数据库连接,则遍历
foreach ($conf as $name => $conn_conf) {
if (($conn_conf['enable'] ?? true) !== false) {
DBPool::create($name, $conn_conf);
}
}
// 读取 Redis 配置文件并创建池
$redis_conf = config('global.redis');
foreach ($redis_conf as $name => $conn_conf) {
if (($conn_conf['enable'] ?? true) !== false) {
RedisPool::create($name, $conn_conf);
}
}
DBPool::resetPools();
RedisPool::resetPools();
}
}

View File

@ -50,7 +50,7 @@ class Framework
public const VERSION_ID = 719;
/** @var string 版本名称 */
public const VERSION = '3.1.13';
public const VERSION = '3.1.14';
/**
* @var RuntimePreferences 运行时偏好(环境信息&参数)

View File

@ -21,6 +21,28 @@ class DBPool
*/
private static array $pools = [];
/**
* 重新初始化连接池,有时候连不上某个对象时候可以使用,也可以定期调用释放链接
*
* @throws DBException
*/
public static function resetPools(): void
{
// 清空 MySQL 的连接池
foreach (DBPool::getAllPools() as $name => $pool) {
DBPool::destroyPool($name);
}
// 读取 MySQL/PostgresSQL/SQLite 配置文件并创建连接池
$conf = config('global.database');
// 如果有多个数据库连接,则遍历
foreach ($conf as $name => $conn_conf) {
if (($conn_conf['enable'] ?? true) !== false) {
DBPool::create($name, $conn_conf);
}
}
}
/**
* 通过配置文件创建一个 MySQL 连接池
*

View File

@ -18,6 +18,27 @@ class RedisPool
*/
public static array $pools = [];
/**
* 重新初始化连接池,有时候连不上某个对象时候可以使用,也可以定期调用释放链接
*
* @throws RedisException
*/
public static function resetPools(): void
{
// 清空 Redis 连接池
foreach (self::getAllPools() as $name => $pool) {
self::destroyPool($name);
}
// 读取 Redis 配置文件并创建池
$redis_conf = config('global.redis');
foreach ($redis_conf as $name => $conn_conf) {
if (($conn_conf['enable'] ?? true) !== false) {
self::create($name, $conn_conf);
}
}
}
/**
* @throws RedisException
*/