java HikariCP 连接池立即创建 100 个连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27396725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:43:56  来源:igfitidea点击:

HikariCP Connection Pool immediately creates 100 connections

javamysqldatabase-connectionconnection-pooling

提问by Peter Andersson

I have this code that uses HikariCP Connection Pool:

我有这个使用 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);

I monitor connections in mysql by issuing command "Show Processlist" and I see that 100 connections is created after line:

我通过发出命令“Show Processlist”来监视 mysql 中的连接,我看到在行之后创建了 100 个连接:

pooledDataSource = new HikariDataSource(config);

...is run. I'm sure this is not meant to happen, right? It should create connections later when I do pooledDataSource.getConnection().

...正在运行。我确定这不会发生,对吧?当我执行 pooledDataSource.getConnection() 时,它应该稍后创建连接。

What am I doing wrong? Why is it creating 100 connections immediately??

我究竟做错了什么?为什么它会立即创建 100 个连接?

回答by brettw

By default HikariCP runs as a fixed-sized pool. You need to set minimumIdle. That's it.

默认情况下,HikariCP 作为固定大小的池运行。您需要设置minimumIdle. 而已。

From the documentation for 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

此属性控制 HikariCP 尝试在池中维护的最小空闲连接数。如果空闲连接低于此值,HikariCP 将尽最大努力快速有效地添加其他连接。但是,为了获得最佳性能和对峰值需求的响应,我们建议不要设置此值,而是允许 HikariCP 充当固定大小的连接池。默认值:与maximumPoolSize 相同