7

Would anyone care to elaborate how the HikariCP handles connections in the pool? How do you put a new connection in the pool, and how can you call on it / retrieve it 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);

1 Answer 1

14

With a pool, you don't add a connection to the pool to retrieve it later. You do the exact inverse: you get a connection from the pool when you need one, and close the connection when you're done with it to give it back to the pool. The HikariDataSource, as its name indicates, is a DataSource. A DataSource is an object from which you get connections.

The pool handles the opening of the connection for you. It puts you in a waiting queue if no connections are available automatically, etc.

Depending on the properties of the pool, the pool can open the connections immediately or on demand, keep a given number of connections always opened, shrink the pool size after given amount of unused time, etc.

That's all very well documented: https://github.com/brettwooldridge/HikariCP#user-content-configuration-knobs-baby

Example code (Java 7 and later):

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

Example code (Before Java 7):

Connection connection = ds.getConnection();
try {
    // use the connection
}
finally {
    connection.close();
}
7
  • Thank you. I have actuallu read that, but I beleive I need some code to understand it fully. Do you have any example code showing it in use ?
    – miniHessel
    Oct 7, 2014 at 6:04
  • You use a HikariDataSource like you would use any other DataSource: call getConnection() on the DataSource to get a connection, then use this connection, then call connection.close() when you're done with the connection. This is standard JDBC.
    – JB Nizet
    Oct 7, 2014 at 6:13
  • Okay, thanks. So even though you are using the connection later, you should write close(), and then getConnection later on?
    – miniHessel
    Oct 7, 2014 at 7:54
  • 11
    The connection that the pool returns to you when calling getConnection() is not the physycal database connection. It's a wrapper (a proxy) around the actual physical connection. Calling close() on this proxy doesn't close the physical connection. All it does is giving back the connection to the pool.
    – JB Nizet
    Oct 7, 2014 at 8:08
  • Ah, great ! Thank you for clarifying. So if I understood correctly, for each HikariDataSource you actually store the Connection Info inside the pool. By calling get connection Hikari "borrows" you the connection, and when you close() you say you are finished with it and then it can be used elsewhere.
    – miniHessel
    Oct 7, 2014 at 8:36

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.