我有这段使用 HikariCP 连接池的代码:
config.setMaximumPoolSize(100);
config.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
config.addDataSourceProperty("serverName", hostname);
config.addDataSourceProperty("port", portnumber);
config.addDataSourceProperty("databaseName", dbname);
config.addDataSourceProperty("user", username);
config.addDataSourceProperty("password", password);
config.setConnectionTimeout(30000);
config.setInitializationFailFast(false);
pooledDataSource = new HikariDataSource(config);
我通过发出命令“Show Processlist”监视 mysql 中的连接,我看到在以下行之后创建了 100 个连接:
pooledDataSource = new HikariDataSource(config);
...运行。我确定这不会发生,对吧?它应该稍后在我执行 pooledDataSource.getConnection() 时创建连接。
我做错了什么?为什么它立即创建 100 个连接??
最佳答案
默认情况下,HikariCP 作为固定大小的池运行。您需要设置 minimumIdle
。就是这样。
来自 minimumIdle
的文档:
This property controls the minimum number of idle connections that HikariCP tries to maintain in the pool. If the idle connections dip below this value, HikariCP will make a best effort to add additional connections quickly and efficiently. However, for maximum performance and responsiveness to spike demands, we recommend not setting this value and instead allowing HikariCP to act as a fixed size connection pool. Default: same as maximumPoolSize
关于java - HikariCP 连接池立即创建 100 个连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27396725/