我分别用以下链接方式测试了一下:tomcat自带的连接池
<property name="connection.datasource">java:comp/env/jdbc/mysqlds</property>JDBC直接连接
<property name="hibernate.connection.url">jdbc:mysql://localhost:3333/message</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">56906950</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>#使用c3p0  连接池
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3333/message</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">56906950</property>
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.min_size">8</property>
<property name="hibernate.c3p0.max_size">200</property>
<property name="hibernate.c3p0.timeout">600</property>
<property name="hibernate.c3p0.max_statements">0</property>
<property name="hibernate.c3p0.idle_test_period">60</property>
<property name="hibernate.c3p0.acquire_increment">2</property>
<property name="hibernate.c3p0.validate">true</property>不知道我上面写的配置是不是正确,但愿不对....
然后写了一个测试方法:
long time=System.currentTimeMillis();
for(int i=0;i<1000;i++){
Session session=HibernateUtils.getSession();
HibernateUtils.closeSession(session);
}
System.out.println(System.currentTimeMillis()-time);发现JDBC所花费的时间是最少的2000ms,其次是tomcat自带的连接池2096,c3p0竟然用了4950ms!不是说c3p0是最成熟的吗?为何是这种情况,还是我写的测试方法不是很有效呢?谢谢啊!

解决方案 »

  1.   

    上面写的测试都是第一次重新打开服务器时所用的时间,刷新页面所用的时间都差不多JDBC和tomcat 连接池一样,而且他们都是c3p0的2倍
      

  2.   

    同意楼上的说法,我们不能用第一次加载的时候所用的时间来判断连接池的效率或是成熟性,我感觉c3p0很好用,我以前遇到过一个问题,mysql自动断开连接时间是8个小时,也就是说8个小时没有访问就自动断开连接池的连接,用c3p0可以解决这个断开,可以先判断连接情况再连接,网上可以查到