Java – Multithreading with Apache DBCP

apache-commons-dbcpconnection-poolingjavamultithreading

My application executes multiple queries in multiple threads.

Right now I am creating new connection for every query and I want to use pool to improve efficiency.

Can Apache DBCP pool work in multiple threads simultaneously, or it will "block" on some synchronized methods per every thread?

If it blocks, can I use something else?

UPDATE

In this article: link stated:

Because all Oracle JDBC API methods are synchronized, if two threads
try to use the connection object simultaneously, then one will be
forced to wait until the other one finishes its use.

So I guess DBCP won't be able to deal with that?

I am also realizing that in this scenario the pool won't help me, because each thread will ask for a connection and the thread will generate a new connection each time (until some of the threads ends and returns the connection to the pool)

Best Answer

A thread takes a connection from the pool and makes exclusive use of it until it is done, it doesn't share the connection with other threads. When it is done it returns the connection to the pool (usually the connection overrides the close method to return it to the pool). The benefit is that the connections don't have to be recreated for each use. But you shouldn't have multiple threads making simultaneous use of a database connection.