I have a standalone java application that gets streams of messages, batches and inserts them to a SQL Server database using Hikaricp.
Currently what I do is the following:
Note that I never close the connection! And once I reach the maxPoolSize (20), I get an error when I tried to get a new connection.
Should I be closing the connection after every batch insert?
However, this means that I am incurring the cost of getting a connection from the pool + creating a new prepared statement after every batch insert.
Is this the recommended approach or are there any alternatives which can reduce this extra cost?
You need to close connection. usually pools return connections wrapped into another object. And when you call close
on wrapper it simply marks internal database connection as free and returns it to the pool.
Also you can reuse existing PreparedStatement
objects if they are the same. If each of your tasks has to use its unique PreparedStatement
(due to your business logic) then there is no other way and you have to create new PreparedStatement
for each task.
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