Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HikariCP multithreading separate connection for each thread

To the folks of the stackoverflow community. I was looking for some help with an issue i am facing with HikariCP connection pooling.

High level: I am trying to create several threads using a thread pool and my plan is to give each worker thread its own separate connection from the HikariCP, but what HikariCP is doing is that it is sharing a common connection across multiple threads. I am using

public synchronized Connection getConnection() throws SQLException
{
  synchronized (dataSource) {
  return dataSource.getConnection();
  }
}

to retrieve a DB connection. Now when I close a connection, I am seeing issues in other threads saying that the connection got closed and the batch of records that thread is processing get dropped.

Here are the stmts from my log file:

2016-06-08 11:52:11,000 DEBUG [com.boxer.delete.SmartProcessWorker] (pool-1-thread-6:) Closing DB connection ConnectionJavassistProxy(1551909727) wrapping oracle.jdbc.driver.T4CConnection@7b4f840f
2016-06-08 11:52:11,002 DEBUG [com.boxer.delete.SmartProcessWorker] (pool-1-thread-9:) Closing DB connection ConnectionJavassistProxy(1511839669) wrapping oracle.jdbc.driver.T4CConnection@343b8714
2016-06-08 11:52:11,014 ERROR [com.boxer.delete.SmartProcessWorker] (pool-1-thread-5:) SQLException trying to Execute pstmt batch
2016-06-08 11:52:11,014 ERROR [com.boxer.delete.SmartProcessWorker] (pool-1-thread-5:) Connection is closed
java.sql.SQLException: Connection is closed
        at com.zaxxer.hikari.proxy.ConnectionProxy.checkClosed(ConnectionProxy.java:275)
        at com.zaxxer.hikari.proxy.ConnectionJavassistProxy.commit(ConnectionJavassistProxy.java)
        at com.boxer.delete.SmartProcessWorker.processRecords(SmartProcessWorker.java:219)
        at com.boxer.delete.SmartProcessWorker.run(SmartProcessWorker.java:174)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:745)

Now, can someone help me with how to get a Db connection from hikari datasource which is not apparently being shared by any other thread ?

like image 842
hercules Avatar asked Jun 08 '16 18:06

hercules


People also ask

Can multiple threads use the same DB connection?

No. Of course not. Each thread needs its own connection.

How does HikariCP connection pool work?

Summary. "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.

What is the use of HikariCP in spring boot?

Hikari is a JDBC DataSource implementation that provides a connection pooling mechanism. Compared to other implementations, it promises to be lightweight and better performing.

What is multi threading issue?

Multithreaded programs allow the execution of multiple parts of a program at the same time. These parts are known as threads and are lightweight processes available within the process. Threads improve the application performance using parallelism. They share information like data segment, code segment files etc.


1 Answers

Short answer: don't do that. JDBC connections are not meant to be shared between threads.

From the author of HikariCP (source):

Multithreaded access to Connections was deprecated in JDBC and is not supported by HikariCP either.

HikariCP is fast enough that you can obtain a Connection, execute SQL, and then return it back to the pool many times in the course of a request.

It is a Best Practice to only hold Connections in local variables, preferably in a try-with-resources block, or possibly passed on the stack, but never in a class member field. If you follow that pattern it is virtually impossible to leak a Connection or accidentally share across threads.

like image 79
rustyx Avatar answered Oct 21 '22 19:10

rustyx