Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HikariCP Connection Pool immediately creates 100 connections

I have this code that uses HikariCP Connection Pool:

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:

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().

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

like image 238
Peter Andersson Avatar asked Dec 10 '14 08:12

Peter Andersson


People also ask

How does HikariCP connection pool work?

"HikariCP is solid high-performance JDBC connection pool. A connection pool is a cache of database connections maintained so that the connections can be reused when future requests to the database are required. Connection pools may significantly reduce the overall resource usage." - You can find out more here.

How many connection pools should I have?

For optimal performance, use a pool with eight to 16 connections per node. For example, if you have four nodes configured, then the steady-pool size must be set to 32 and the maximum pool size must be 64.

What is advantage of connection pooling?

Benefits of connection pooling Connection pooling can improve the response time of any application that requires connections, especially Web-based applications. When a user makes a request over the web to a resource, the resource accesses a data source.


1 Answers

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

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

like image 118
brettw Avatar answered Oct 18 '22 21:10

brettw