Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multithreading with Apache DBCP

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)

like image 386
user1658192 Avatar asked Sep 09 '12 14:09

user1658192


People also ask

How does DBCP connection pool work?

The connection user name to be passed to our JDBC driver to establish a connection. The connection password to be passed to our JDBC driver to establish a connection. The connection URL to be passed to our JDBC driver to establish a connection. The fully qualified Java class name of the JDBC driver to be used.

What is Apache DBCP?

The DBCP ComponentMany Apache projects support interaction with a relational database. Creating a new connection for each user can be time consuming (often requiring multiple seconds of clock time), in order to perform a database transaction that might take milliseconds.

What is DBCP in Tomcat?

Tomcat DBCP is just a renamed version of Apache Commons DBCP, with also a different internal package name prefix. At build time, Tomcat fetches the Commons DBCP sources (the version depends on the Tomcat version, for instance Tomcat 7.0.

What is org Apache Commons DBCP BasicDataSource?

org. apache. commons. dbcp. BasicDataSource is actually a connection pool, from which you can borrow/return connections to any flavour of database: Oracle, Sybase, DB2, etc.


2 Answers

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.

like image 128
Nathan Hughes Avatar answered Oct 08 '22 22:10

Nathan Hughes


Yes, Apache DBCP can work in multi-threads simultaneously. "block" happens when client code getConnection() to ensure correct behavior under race condition, for example, one Connection instance should not be got by two concurrent getConnection() requests. After that client code handles the Connection instances.

Concurrent scenario is major concern at server side pooling, such as popular Apache DBCP. So I think DBCP does well behavior in multi-thread, although I don't dive into the library deep.

And Apache DBCP just provides JDBC connection pooling, client code must use the Connection instances in correct multi-threads way which DBCP cannot guarantee.

like image 26
卢声远 Shengyuan Lu Avatar answered Oct 08 '22 21:10

卢声远 Shengyuan Lu