以前一直用apache的dbcp来做Datasource,最近偶然发现了tomcat的jdbc-pool,然后研究了下,毅然决定换为tomcat的,这里有很多原因,首先看apache官网:http://people.apache.org/~fhanik/jdbc-pool/jdbc-pool.html
大致如下:
So why do we need a new connection pool?
Here are a few of the reasons:
- commons-dbcp is single threaded, in order to be thread safe commons-dbcp
locks the entire pool, even during query validation.
- commons-dbcp is slow - as the number of logical CPUs grow, the performance
suffers, the above point shows that there is not support for high concurrency
Even with the enormous optimizations of the
synchronized
statement
in Java 6, commons-dbcp still suffers in speed and concurrency.
- commons-dbcp is complex, over 60 classes. tomcat-jdbc-pool, core is 8
classes, hence modifications for future requirement will require much less
changes. This is all you need to run the connection pool itself, the rest is
gravy.
- commons-dbcp uses static interfaces. This means you can't compile it with
JDK 1.6, or if you run on JDK 1.6/1.7 you will get NoSuchMethodException for all
the methods not implemented, even if the driver supports it.
- The commons-dbcp has become fairly stagnant. Sparse updates, releases, and
new feature support.
- It's not worth rewriting over 60 classes, when something as a connection
pool can be accomplished with as a much simpler implementation.
- Tomcat jdbc pool implements a fairness option not available in commons-dbcp
and still performs faster than commons-dbcp
- Tomcat jdbc pool implements the ability retrieve a connection
asynchronously, without adding additional threads to the library itself
- Tomcat jdbc pool is a Tomcat module, it depends on Tomcat JULI, a simplified
logging framework used in Tomcat.
- Retrieve the underlying connection using the javax.sql.PooledConnection
interface.
- Starvation proof. If a pool is empty, and threads are waiting for a
connection, when a connection is returned, the pool will awake the correct
thread waiting. Most pools will simply starve.
Features added over other connection pool implementations
- Support for highly concurrent environments and multi core/cpu systems.
- Dynamic implementation of interface, will support java.sql and javax.sql
interfaces for your runtime environment (as long as your JDBC driver does the
same), even when compiled with a lower version of the JDK.
- Validation intervals - we don't have to validate every single time we use
the connection, we can do this when we borrow or return the connection, just not
more frequent than an interval we can configure.
- Run-Once query, a configurable query that will be run only once, when the
connection to the database is established. Very useful to setup session
settings, that you want to exist during the entire time the connection is
established.
- Ability to configure custom interceptors. This allows you to write custom
interceptors to enhance the functionality. You can use interceptors to gather
query stats, cache session states, reconnect the connection upon failures, retry
queries, cache query results, and so on. Your options are endless and the
interceptors are dynamic, not tied to a JDK version of a java.sql/javax.sql
interface.
- High performance - we will show some differences in performance later on
- Extremely simple, due to the very simplified implementation, the line count
and source file count are very low, compare with c3p0 that has over 200 source
files(last time we checked), Tomcat jdbc has a core of 8 files, the connection
pool itself is about half that. As bugs may occur, they will be faster to track
down, and easier to fix. Complexity reduction has been a focus from inception.
- Asynchronous connection retrieval - you can queue your request for a
connection and receive a Future<Connection> back.
- Better idle connection handling. Instead of closing connections directly, it
can still pool connections and sizes the idle pool with a smarter algorithm.
- You can decide at what moment connections are considered abandoned, is it
when the pool is full, or directly at a timeout by specifying a pool usage
threshold.
- The abandon connection timer will reset upon a statement/query activity.
Allowing a connections that is in use for a long time to not timeout. This is
achieved using the ResetAbandonedTimer
- Close connections after they have been connected for a certain time. Age
based close upon return to the pool.
- Get JMX notifications and log entries when connections are suspected for
being abandoned. This is similar to the
removeAbandonedTimeout
but
it doesn't take any action, only reports the information. This is achieved using
the suspectTimeout
attribute.
- Connections can be retrieved from a
java.sql.Driver
,
javax.sql.DataSource
or javax.sql.XADataSource
This is
achieved using the dataSource
and dataSourceJNDI
attributes.
- XA connection support
然后
好,以后就用tomcat jdbc pool了
配置非常简单,给个例子:
<!-- data source configs -->
<dataSource id="ecifDataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
<param name="driverClassName">com.ibm.db2.jcc.DB2Driver</param>
<param name="url">jdbc:db2://localhost:50000/POCFI</param>
<param name="username">ecif</param>
<param name="password">ecif</param>
<param name="initialSize">3</param>
<param name="maxActive">12</param>
<param name="maxIdle">3</param>
<param name="minIdle">3</param>
<param name="validationQuery">SELECT 1 FROM SYSIBM.SYSDUMMY1</param>
<param name="testOnBorrow">true</param>
<param name="testOnReturn">true</param>
<param name="testWhileIdle">true</param>
</dataSource>
分享到:
相关推荐
标题中的"jdbc-dbcp-c3p0.jar"是一款用于数据库连接池管理的Java库,它主要包含Apache的DBCP(数据库连接池)和C3P0两个组件。这两个组件都是在Java应用程序中管理和复用数据库连接的重要工具,提高了数据库操作的...
DBCP(Jakarta DBCP)和c3p0是两种常用的开源数据库连接池实现。 DBCP,全称Jakarta Commons DBCP,是Apache软件基金会下的一个项目,它基于Apache的Jakarta POI项目中的DBUtils模块发展而来。DBCP提供了一个可配置...
C3P0,全称为Compositional JDBC Connection Pool,是由M-Fusion公司开发的另一个开源数据库连接池。C3P0的特点包括: 1. **连接池扩展**:C3P0提供了更丰富的连接池配置选项,如连接测试策略、自动空闲检测频率、...
本压缩包"commons-dbcp2-pool-c3p0.rar"包含了几个主要的数据库连接池实现,包括Apache的Commons DBCP2和C3P0,以及相关的依赖库。 首先,Apache Commons DBCP2(Database Connection Pool 2)是一个开源的、基于...
现代的替代品如HikariCP和C3P0提供了更高的性能和更好的资源管理,对于新的项目,可能需要考虑使用这些更新的连接池实现。 总结来说,Apache Commons DBCP和Apache Commons Pool是Java开发中用于数据库连接池管理的...
通常,应用程序会使用像C3P0或DBCP2这样的连接池来创建和管理数据库连接,而Commons Pool2作为底层的池化服务,提供连接的创建和回收。同时,mysql-connector-java则负责实际的数据库通信,将SQL语句发送到MySQL...
- 相比于Tomcat JDBC Connection Pool,c3p0更适合小型项目,大型项目可能会倾向于选择更高效、更活跃维护的连接池。 7. **常见问题与解决方案** - 如果出现“Too many connections”错误,可能是因为`maxPool...
"连接池(内含dbcp c3p0)jar包" 提供了两个著名的Java数据库连接池实现:Apache DBCP(Database Connection Pool)和C3P0,这两个库的jar文件被封装在一起,便于开发者快速引入到他们的项目中。 **Apache DBCP** ...
- C3P0:作为一款开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展。 2. **C3P0功能特点**: - 自动管理连接:C3P0可以自动检测并修复失效的数据库连接,防止因长时间未使用的连接...
c3p0提供了一些高级特性,如自动检测并关闭无效的连接、连接池的配置参数调整、多线程环境下的性能优化等。 DBCP(Apache Commons DBCP)是Apache软件基金会的一个项目,它基于Jakarta Pool(现在的Commons Pool)...
本主题将深入探讨两种流行的数据库连接池实现:DBCP(Jakarta DB Connection Pool)和C3P0,以及如何配置它们。 **DBCP(Jakarta DB Connection Pool)** DBCP是Apache软件基金会的一个项目,提供了一个符合Java ...
本篇将详细讲解使用JDBC进行数据库操作的不同方法,包括不使用连接池以及使用c3p0、dbcp和JNDI等连接池技术。 首先,不使用连接池的JDBC操作通常包含以下步骤: 1. 加载驱动:通过`Class.forName()`加载对应的...
与DBCP、Druid等其他连接池相比,c3p0在性能上有一定的优势,特别是在处理长时间无操作的连接时,其自动检测和恢复机制更为完善。但需要注意的是,c3p0的活跃度不如其他一些连接池,更新较慢,可能无法及时跟进最新...
本资源包包含三个主流的数据库连接池实现:c3p0、dbcp和Druid。它们各有特点,适应不同的应用场景。 1. **c3p0**:全称为Com形3P0,是由Miquel Arquero开发的一个开源的JDBC连接池。c3p0提供了完善的连接池管理和...
4. **性能优化**:c3p0支持连接池的配置参数调整,如最大连接数、最小连接数、空闲连接测试频率等,以适应不同应用的性能需求。 5. **异常处理**:c3p0能够检测到数据库连接的异常,并自动关闭损坏的连接,确保应用...
c3p0与DBCP、HikariCP等其他连接池相比,各有优缺点。c3p0在连接管理上相对复杂,但功能较全;HikariCP则以高效和低延迟著称,但在某些场景下可能不如c3p0稳定。 6. **最佳实践** - 根据应用的实际情况调整配置...
### DBCP与C3P0连接池常用配置参数详解 #### 一、DBCP连接池配置参数 DBCP(Database Connection Pool)是Apache提供的一个开源数据库连接池实现,广泛应用于Java应用程序中以提高数据库访问效率。以下是DBCP中...
在实际应用中,开发人员可以根据需求选择使用DBCP或C3P0,或者与其他数据库连接池库如HikariCP、Druid等结合。这些库通常通过JDBC API与各种数据库进行交互,因此它们可以与多种数据库管理系统兼容,如MySQL、Oracle...
2. **c3p0 jar包**: 使用c3p0连接池,需要引入c3p0的jar包,该包包含了c3p0的所有类和接口,使得我们可以方便地在项目中使用c3p0的功能。 ### c3p0的下载与集成 1. **下载**: c3p0的jar包可以在官方网站或者通过...
`c3p0-config.xml`是c3p0的XML配置文件,提供了更灵活的方式来定义连接池的配置,可以替代`c3p0.properties`。 配置示例: - 对于c3p0,你可以在`c3p0-config.xml`中配置如下: ```xml <data-source class=...