数据库连接池c3p0 ,Proxool ,Druid ,Tomcat Jdbc Pool对比测试

kkikiako 2019-06-29

几种驱动在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,常见的JDBCDriver都包括了。

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_statements">150</property>
    <property name="hibernate.c3p0.acquire_increment">3</property>
    <property name="hibernate.connection.driver_class">org.hibernate.dialect.SQLServerDialect</property>
    <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
    <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>

        <!--实体-->
    <mapping class="gk.data.entities.User" />
    ......
</session-factory>
</hibernate-configuration>

JNDI+dbcp连接池

<hibernate-configuration>
<session-factory>

<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
    <property name="connection.datasource">java:comp/env/jdbc/ManageDB</property>

   <!--实体映射-->
<mapping class="com.data.entities.User" />
.....
</session-factory>
</hibernate-configuration>
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="java:comp/env/jndi/rh_cpm">   </property>
    </bean>

在项目的webRoot/META-INF文件夹下面新建context.xml或者tomcat的conf/context.xml添加如下内容

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE XML>

<Context path="/Manage" docBase="Manage" debug="5" reloadable="true"
    crossContext="true" >
    <!-- 数据库连接 begin-->    
    <!-- 被操作库 --> 
    <Resource name="jdbc/ManageDB" 
        auth="Container"
        type="javax.sql.DataSource" 
        maxActive="5" 
        maxIdle="5" 
        maxWait="5000" 
        factory="org.apache.commons.dbcp.BasicDataSourceFactory" 
        driverClassName="net.sourceforge.jtds.jdbc.Driver"
        url="jdbc:jtds:sqlserver://192.168.1.100:1433/TestDB"
        username="sa" 
        password="sa"  
        testOnBorrow="true" 
        testWhileIdle="true" 
        validationQuery="select getdate()" />
</Context>

连接池c3p0,Proxool,Druid,TomcatJdbcPool对比测试[/url]:[url]http://286.iteye.com/blog/1920417,原文没有jdts的测试。

以下就是此次测试结果(本结果只供参考,更具体更准确的测试可以自行进行):

1.测试次数为10次时:

平均用时(单位:毫秒):

c3p032.26

Proxool33.42

Druid30.43

TomcatJdbcPool37.61

2.测试次数为100次时:

平均用时(单位:毫秒):

c3p0409.94

Proxool447.49

Druid382.7

TomcatJdbcPool386.3

3.测试次数为500次时:

平均用时(单位:毫秒):

c3p01700.95

Proxool2053.85

Druid1777.36

TomcatJdbcPool1749.02

4.测试次数为1000次时:

平均用时(单位:毫秒):

c3p03549.29

Proxool3435.8

Druid3167.59

TomcatJdbcPool3162.25

相关推荐