java - HikariCP 未能初始化池:“致命:抱歉,已经有太多客户”
问题描述
我正在使用 PostGreSQL 数据库来存储数据。(我使用 hikariCP btw)我有很多方法可以从 Hikari 数据源收集连接:
public Connection getConnection() {
try {
return dataSource.getConnection();
}
catch (SQLException ex)
{
return null;
}
}
并使用它们来执行各种功能。最近,我遇到了当数据库接收到很多连接时发生的错误:
com.zaxxer.hikari.pool.HikariPool$PoolInitializationException: Failed to initialize pool: FATAL: sorry, too many clients already
Caused by: org.postgresql.util.PSQLException: FATAL: sorry, too many clients already
我已经尝试编辑我的配置、搜索谷歌并运行测试,但他们没有修复它。这里有人可以帮我解决它,或提供有关如何解决的信息吗?这是我在数据库类构造函数中的配置:
PGSimpleDataSource ds = new PGSimpleDataSource();
ds.setDatabaseName("BGWW Bot");
ds.setServerNames(new String[] {"localhost"});
ds.setUser(dbUser);
ds.setPassword(dbPass);
// Configure the HikariDatesource; add PGDatasource
config.setJdbcUrl("jdbc:postgresql://localhost/BGWW Bot");
config.setMinimumIdle(5);
config.setMaximumPoolSize(100);
config.setMaxLifetime(30000);
config.setIdleTimeout(30000);
config.setConnectionTimeout(60000);
config.setLeakDetectionThreshold(10000);
config.addDataSourceProperty("max_connections", "100");
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
config.setDataSource(ds);
这是一个获取连接的方法:
public String[] getMember(String uuid, TextChannel channel)
{
try (Connection conn = getConnection())
{
String getUserSTR = "SELECT * FROM guild_members WHERE uuid = ?";
PreparedStatement stmt = conn.prepareStatement(getUserSTR);
stmt.setString(1, uuid);
ResultSet rs = stmt.executeQuery();
while(rs.next())
{
if(rs.getString(1).equals(uuid))
{
String[] array = new String[]{rs.getString(1), rs.getString(2), rs.getString(3)};
conn.close();
return array;
}
}
}
catch (SQLException ex)
{
ex.printStackTrace();
return null;
}
return null;
}
我还发现了一些有趣的东西,我在其中快速打开然后关闭新连接 150 次,但它们没有产生任何错误。
什么可能导致此错误?我该如何解决?谢谢!
解决方案
推荐阅读
- debugging - 是否可以调试 Intel TSX?
- powershell - 绕过 Powershell 运行 zsh 命令
- bash - 如何在现有 txt 文件中的 Bash 中编写第二列
- python - 如何从 python 中的正则表达式创建嵌套字典?
- django - Django中的复杂过滤/子查询
- sql - 如果满足条件,则删除所有重复项
- google-apps-script - 在整个工作表中查找具有特定值的单元格的功能
- flutter - 更新到 Flutter 1.12.13 后,TextField 提示/输入文本没有居中且没有 prefixIcon
- reactjs - 在反应测验中设置子组件的数组状态并将其传递回父组件时遇到问题
- sql - SQL 问题:重复键值违反唯一约束