# 什么是 Hibernate 的 Connection pools

**URL:** <https://www.isharkfly.com/t/hibernate-connection-pools/6493>\
**Category:** 程序语言\
**Created:** [May 24, 2011, 5:35pm UTC](https://www.isharkfly.com/t/hibernate-connection-pools/6493 "2011-05-24T17:35:02Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [May 24, 2011, 5:35pm UTC](https://www.isharkfly.com/t/hibernate-connection-pools/6493/1 "2011-05-24T17:35:02Z")

</div>

连接池是提供系统性能常用的一个方法。

使用连接池后不再需要为每一次 SQL 执行创建一个单独的连接。

数据库连接池为你打开的数据库连接维持连续属性。应用服务器使用JNDI DataSource，通常提供自己的连接池。这样 Hibernate 将会使用连接池提供的各种高级性能。

当你选择一个连接池属性的时候，你必须配置你自己的环境。

Hibernate 通过 hibernate.cfg.xml 文件来支持连接池属性。

connection.provider\_class 属性设置连接池实现。`<property name="connection.provider_class">
    org.hibernate.connection.C3P0ConnectionProvider
</property>`1).使用Hibernate自带的连接池。

2).使用配置文件指定的第三方数据库连接池,官方推荐的连接池是C3P0,Proxool,以及DBCP。

3).从容器中获取得到连接池（如：Tomcat）,这种方法很少用,属于服务器的配置,在此不做表述.

1.自带的连接池  
如果不做配置,默认情况下使用的是自带的连接池,但这个连接池性能不佳,而且有很多BUG,建议只在开发环境中使用.可选配置:` hibernate.connection.pool_size 5 `2. DBCP连接池:  
DBCP连接池由于存在重大BUG,在Hibernate3中已被抛弃,故在此也抛弃之.

3.C3P连接池：  
虽然C3P的算法不是最优的，而且占用资源大，但由于配置简单，可以作为首选的连接池。`hibernate.connection.provider_class org.hibernate.connection.C3P0ConnectionProvider  
hibernate.c3p0.max_size 2  
hibernate.c3p0.min_size 2  
hibernate.c3p0.timeout 5000  
hibernate.c3p0.max_statements 100  
hibernate.c3p0.idle_test_period 3000 [code] <?xml version="1.0" encoding="utf-8"?>  
    <!-- the proxool configuration can be embedded within your own application's.  
    Anything outside the "proxool" tag is ignored. -->  
    <something-else-entirely>  
    <proxool>  
    <alias>pool1</alias>  
    <!--proxool只能管理由自己产生的连接-->  
    <driver-url>jdbc:mysql://localhost:3306/struts?useUnicode=true&characterEncoding=GBK</driver-url>  
    <driver-class>org.gjt.mm.mysql.Driver</driver-class>  
    <driver-properties>  
    <property name="user" value="root"/>  
    <property name="password" value="8888"/>  
    </driver-properties>  
    <!-- proxool自动侦察各个连接状态的时间间隔(毫秒),侦察到空闲的连接就马上回收,超时的销毁-->  
    <house-keeping-sleep-time>90000</house-keeping-sleep-time>  
    <!-- 指因未有空闲连接可以分配而在队列中等候的最大请求数,超过这个请求数的用户连接就不会被接受-->  
    <maximum-new-connections>20</maximum-new-connections>  
    <!-- 最少保持的空闲连接数-->  
    <prototype-count>5</prototype-count>  
    <!-- 允许最大连接数,超过了这个连接，再有请求时，就排在队列中等候，最大的等待请求数由maximum-new-connections决定-->  
    <maximum-connection-count>100</maximum-connection-count>  
    <!-- 最小连接数-->  
    <minimum-connection-count>10</minimum-connection-count>  
    </proxool>  
    </something-else-entirely> `需要proxool-0.8.3.jar  
hibernate.c3p0.acquire\_increment 2  
hibernate.c3p0.validate false [/code]

需要c3p0-0.8.4.5.jar

4.Proxool连接池：  
此连接池负面评价最少，性能也是最好的。但配置颇为复杂，建议在性能优化时使用。` hibernate.connection.provider_class org.hibernate.connection.ProxoolConnectionProvider  
    hibernate.proxool.pool_alias pool1  
    hibernate.proxool.xml proxool.xml `proxool.xml
