Which Java Connection Pool Wins? Druid vs C3P0 vs DBCP Performance Tested
This article introduces the importance of database connections, compares three popular Java connection pools (DBCP, C3P0, Druid) with code examples, runs performance tests inserting 100 k and 1 M rows on MySQL, Oracle and PostgreSQL, and concludes with recommendations on the most stable pool and optimal databases.
1. Introduction
Database connections are a critical, limited, and expensive resource, especially in multi‑user web applications. A past incident with a c3p0 pool limited to 500 connections caused massive insert failures under high concurrency, prompting a thorough stability test of various data source connection pools.
2. Code Example
Common Java open‑source connection pools include:
dbcp : relies on Jakarta commons‑pool; Tomcat’s datasource uses DBCP.
c3p0 : open‑source JDBC pool bundled with Hibernate, supporting jdbc3 and jdbc2 specifications.
druid : Alibaba’s pool with ProxyDriver, built‑in JDBC components and SQL parser, supporting Oracle, MySQL, Derby, PostgreSQL, SQL Server, H2, etc.
Test table creation script:
<code>CREATE TABLE t_test (
id bigint(20) unsigned NOT NULL COMMENT '主键ID',
name varchar(32) NOT NULL COMMENT '名称',
PRIMARY KEY (id)
) ENGINE=InnoDB COMMENT='测试表';</code>DBCP configuration file (dbcp‑jdbc.properties):
<code>username=root
password=Hello@123456
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://192.168.31.200:3306/testdb?useUnicode=true&characterEncoding=UTF-8
initialSize=5
maxActive=1000
maxIdle=5
removeAbandoned=true
removeAbandonedTimeout=20
logAbandoned=true
maxWait=100</code>Utility class
DbcpJdbcUtilprovides methods to obtain a datasource, get connections, begin/commit/rollback transactions, and release connections.
<code>public class DbcpJdbcUtil {
private static final Logger logger = LoggerFactory.getLogger(DbcpJdbcUtil.class);
private static Properties prop = new Properties();
private static BasicDataSource dataSource = null;
private static ThreadLocal<Connection> tl = new ThreadLocal<>();
static { classPathSourceRead(); }
// ... (methods getDataSource, getConnection, beginTransaction, commitTransaction, rollbackTransaction, releaseConnection)
}</code>Unit test class
DBCPTestruns single‑threaded and multi‑threaded insert operations.
<code>public class DBCPTest {
private static final int sumCount = 1000000;
private static final int threadNum = 600;
// ... (setup, testMysql, testThreadMysql)
}</code>3. Performance Test
Tests were executed on MySQL 5.7, Oracle 12 and PostgreSQL 9.6 using the three pools. Inserting 100 000 rows and 1 000 000 rows yielded the following observations (images omitted):
From a connection‑pool perspective: DBCP ≥ Druid > C3P0 for 100 k rows; Druid is the most stable for 1 M rows, while C3P0 shows execution failures.
From a database perspective: Oracle > PostgreSQL > MySQL for 100 k rows; PostgreSQL > Oracle > MySQL for 1 M rows.
Overall, Druid and DBCP have comparable performance, but Druid offers better stability under high concurrency. PostgreSQL demonstrates the highest insertion speed among the databases, followed by Oracle and MySQL.
4. Conclusion
For real‑world development, the author recommends using the Druid connection pool and PostgreSQL as the primary database, with Oracle and MySQL as secondary options.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.