Understanding the Database Connection Pool (DBCP) Properties
Read here to understand which database connection property is being used in which scenario. The clarity in this aspect will make it easy to tune these values.
Join the DZone community and get the full member experience.
Join For FreeRecently, I faced an issue related to a very high load on the database layer. The database was having too many connections in parallel. I had to review my application’s database connection pool (DBCP) properties very closely. Since I was dealing with legacy code, I needed to understand the value assigned to each property and also analyze whether it is relevant for the present-day load or not. As I started looking at the properties, their values, and the consequent implications, I was able to find a decent explanation in the tomcat documentation. However, I wasn’t able to immediately map each property to the scenario where it will be used.
Since we were using Apache tomcat’s JDBC connection pool, I started reading the source code to get a better understanding. I was able to get a lot of clarity by going through the ConnectionPool class. As I didn’t find any easy resource to understand the same, I am summarizing my understanding in the form of simple flowcharts. I hope this will help others in a similar situation.
I am not covering every property. I looked at the major properties such as, initialSize
, maxActive
, maxWait
, maxIdle
, timeBetweenEvictionRunsMillis
, removeAbandoned
, removeAbandonedTimeout
, and minEvictableIdleTimeMillis
.
Of these, initialSize
is the simplest. It is the number of connections created by the Connection Pool Manager at the start. If it is not defined, the Connection Pool Manager will create 10 threads by default.
For the other properties, I have recognized three scenarios where they will be used. Let’s go through each scenario and understand how these properties are used.
Scenario 1: Get a Data Source Connection
It involves the usage of maxActive
(default value = 100) and maxWait
(default value = 30 seconds).
Scenario 2: A Data Source Connection Is Released by the Application Thread
It involves the usage of maxIdle
property. The default value of maxIdle
is the value assigned to maxActive.
If maxActive
is not set, we set the same default value of 100 to both these properties.
Scenario 3: Pool Cleaner Run
Pool Cleanup is performed at regular intervals to maintain the connections. It takes care of the abandoned connections as well as the idle connections.
It involves the usage of timeBetweenEvictionRunsMillis
(default value = 5 seconds), removeAbandoned
(false by default), removeAbandonedTimeout
(default value = 60 seconds), minIdle
(default value = value of initialSize. In case it isn’t defined, both are set to 10), and minEvictableIdleTimeMillis
(default value = 60 seconds) properties.
Note: There are different implementations for the database connection pool. The code referred to in this article is an implementation by Apache Tomcat. For folks who are interested in the alternatives, Apache Commons has an implementation as well.
Published at DZone with permission of Diwakar Grandhi. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments