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??
"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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With