Should I close the HikariDataSource after each SQL query?

5,837 views
Skip to first unread message

Jen-Ming Chung

unread,
Mar 23, 2015, 3:35:13 AM3/23/15
to hika...@googlegroups.com
Hi everyone,

Do I need to create the HikariDataSource and close it for each SQL query? If I did, I always get the following results on console:

INFO  [2015-03-23 07:26:31,202] com.zaxxer.hikari.HikariDataSource: HikariCP pool HikariPool-0 is starting.
...
INFO  [2015-03-23 07:26:31,524] com.zaxxer.hikari.pool.HikariPool: HikariCP pool HikariPool-0 is shutting down.
INFO  [2015-03-23 07:26:31,528] com.zaxxer.hikari.util.ConcurrentBag: ConcurrentBag has been closed, ignoring add()

And the HikariPool-# will be increased with further queries.

I found the hikaricp-test project on Github, and I can use the ConnectionPool.java to get a connection to execute SQL query.
The console will only show the pool message once without the extra shutting down messages.

May I ask which way is the correct way to use the Hikari connection pool?


Many thanks,
Jeremy



Brett Wooldridge

unread,
Mar 23, 2015, 10:22:59 AM3/23/15
to hika...@googlegroups.com
You absolutely do not want to create the HikariDataSource and close it for each SQL query.  You want to create a HikariDataSource at application startup, probably as a singleton, and close it only at application shutdown.

When you need to execute a SQL query, you call Connection connection = ds.getConnection() to get a Connection (where ds is your single instance of HikariDataSource).   After the query, call connection.close() when you are finished with it.

-Brett

Jen-Ming Chung

unread,
Mar 23, 2015, 7:30:17 PM3/23/15
to hika...@googlegroups.com
Hi Brett,

Thank you very much for your timely and detailed reply. ;-)

-Jeremy 
Reply all
Reply to author
Forward
0 new messages