How to create new connections and get them later in HikariCP

Would it be interesting how HikariCP handles connections in the pool? How do you add a new connection to the pool and how can it be called / received later?

This is my current code:

HikariConfig config = new HikariConfig(); config.setMaximumPoolSize(100); config.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource"); config.addDataSourceProperty("serverName", "localhost"); config.addDataSourceProperty("port", "8889"); config.addDataSourceProperty("databaseName", "XXX"); config.addDataSourceProperty("user", "XXX"); config.addDataSourceProperty("password", "XXX"); System.out.print("qq"); HikariDataSource ds = new HikariDataSource(config); ds.setConnectionTimeout(800); 
+7
java hikaricp
source share
1 answer

With a pool, you do not add a connection to the pool to receive it later. You do the exact opposite: you get a connection from the pool when you need it, and close the connection when you are done with it to return it to the pool. HikariDataSource, as its name indicates, is a data source. DataSource is the object from which you get the connections.

The pool handles opening a connection for you. It puts you on a waiting queue if connections are not automatically available, etc.

Depending on the properties of the pool, the pool can open connections immediately or on demand, constantly open a certain number of connections, reduce the size of the pool after a given amount of unused time, etc.

This is very well documented: https://github.com/brettwooldridge/HikariCP#user-content-configuration-knobs-baby

Sample code (Java 7 and later):

 try (Connection connection = ds.getConnection()) { // use the connection } 

Sample code (before Java 7):

 Connection connection = ds.getConnection(); try { // use the connection } finally { connection.close(); } 
+11
source share

All Articles