首页 > 解决方案 > 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 次,但它们没有产生任何错误。

什么可能导致此错误?我该如何解决?谢谢!

标签: javasqlpostgresqljdbchikaricp

解决方案


推荐阅读