日期:2014-05-16  浏览次数:20570 次

连接池c3p0 ,Proxool ,Druid ,Tomcat Jdbc Pool对比测试
几种驱动在Hibernate的配置:
Druid: Hibernate操作数据库文件不会报错,jdts容易报错。
为了方便大家迁移,Druid的配置和DBCP是基本一致的,如果你原来是使用DBCP,迁移是十分方便的,只需要把corg.apache.commons.dbcp.BasicDataSource修改为om.alibaba.druid.pool.DruidDataSource就好了。
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> 
<!-- <bean id="dataSource" class="java.sql.DriverManager.BasicDataSource" destroy-method="close">-->
    <property name="url" value="jdbc:sqlserver://192.168.0.167:1433;DatabaseName=test" />
    <property name="username" value="sa" />
    <property name="password" value="sa" />
     
    <property name="filters" value="stat" />
 
    <property name="maxActive" value="20" />
    <property name="initialSize" value="1" />
    <property name="maxWait" value="60000" />
    <property name="minIdle" value="1" />
 
    <property name="timeBetweenEvictionRunsMillis" value="60000" />
    <property name="minEvictableIdleTimeMillis" value="300000" />
 
    <property name="validationQuery" value="SELECT 'x'" />
    <property name="testWhileIdle" value="true" />
    <property name="testOnBorrow" value="false" />
    <property name="testOnReturn" value="false" />
     
    <property name="poolPreparedStatements" value="true" />
    <property name="maxPoolPreparedStatementPerConnectionSize" value="50" />
</bean>

在上面的配置中,通常你需要配置url、username、password、maxActive这几项。
在DruidDataSource中,你可以不配置DriverClass,它根据url自动识别。Druid能够自动识别20多中url,常见的JDBC Driver都包括了。

JDTS:
<bean id="dataSource" class="java.sql.DriverManager.BasicDataSource" destroy-method="close">
    <!-- <bean id="dataSource" class="com.rh.RHBasicDataSource" destroy-method="close">-->
        <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"/>
        <property name="url" value="jdbc:jtds:sqlserver://192.168.0.167:1433/test"/>
        <property name="username" value="sa"/>
        <property name="password" value="sa"/>
        <property name="maxActive" value="600"/>
        <property name="maxWait" value="10000"/>
        <property name="minIdle" value="500"/>
        <property name="poolPreparedStatements" value="true"/>
        <property name="defaultAutoCommit" value="true"/>
        <property name="validationQuery" value="SELECT 1+1"/>
        <property name="testOnBorrow" value="true"/>
    </bean>


c3p0
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>
<session-factory>
    <property name="connection.username">sa</property>
    <property name="connection.url">jdbc:sqlserver://192.168.1.100:1433;databaseName=TestDB</property>
    <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
    <property name="connection.password">sa</property>
    <property name="hibernate.c3p0.max_size">50</property>
    <property name="hibernate.c3p0.min_size">5</property>
    <property name="hibernate.c3p0.timeout">100</property>
    <property name="hibernate.c3p0.idle_test_period">100</property>
    <property name="hibernate.c3p0.max_stateme