使用jdbc-pool替代dbcp、c3p0等连接池的原因

以前一直用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:

  1. commons-dbcp is single threaded, in order to be thread safe commons-dbcp locks the entire pool, even during query validation.
  2. 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.
  3. 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.
  4. 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.
  5. The commons-dbcp has become fairly stagnant. Sparse updates, releases, and new feature support.
  6. It's not worth rewriting over 60 classes, when something as a connection pool can be accomplished with as a much simpler implementation.
  7. Tomcat jdbc pool implements a fairness option not available in commons-dbcp and still performs faster than commons-dbcp
  8. Tomcat jdbc pool implements the ability retrieve a connection asynchronously, without adding additional threads to the library itself
  9. Tomcat jdbc pool is a Tomcat module, it depends on Tomcat JULI, a simplified logging framework used in Tomcat.
  10. Retrieve the underlying connection using the javax.sql.PooledConnection interface.
  11. 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

  1. Support for highly concurrent environments and multi core/cpu systems.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. High performance - we will show some differences in performance later on
  7. 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.
  8. Asynchronous connection retrieval - you can queue your request for a connection and receive a Future<Connection> back.
  9. Better idle connection handling. Instead of closing connections directly, it can still pool connections and sizes the idle pool with a smarter algorithm.
  10. 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.
  11. 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
  12. Close connections after they have been connected for a certain time. Age based close upon return to the pool.
  13. 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.
  14. 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.
  15. XA connection support


    然后

    Why another connection pool project这篇文章详细介绍阐述dbcp与c3p0的一些不足.


    再然后,看看springsource社区:

    http://static.springsource.com/projects/tc-server/6.0/admin/radmjdbc.html

    http://ebr.springsource.com/repository/app/bundle/detail?name=com.springsource.org.apache.tomcat.jdbc

    以及 :
    http://www.grails.org/plugin/jdbc-pool

     

    好,以后就用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>
     

猜你喜欢

转载自guosxu.iteye.com/blog/1270522