c3p0连接池:Hibernate 中配置C3P0连接池

        Hibernate自带连接池算法相当不成熟 它只是为了让你快些上手并不适合用于产品系统或性能测试中 出于最佳性能和稳定性考虑你应该使用第 3方连接池只需要用特定连接池设置替换 hibernate.connection.pool_size即可这将关闭Hibernate自带连接池 例如 你可能会想用C3P0.         C3P0是个随Hibernate同分发开源JDBC连接池 它位于lib目录下 如果你设置了hibernate.c3p0.*相关属性 Hibernate将使用 C3P0ConnectionProvider来缓存CacheJDBC连接 如果你更原意使用Proxool 请参考发 行包中hibernate.properties并到Hibernate网站WebSite获取更多信息

        这是个使用C3P0hibernate.properties样例文件(来自Hibernate包中etc目录下):

###########################
### C3P0 Connection Pool###
###########################

#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
#hibernate.c3p0.acquire_increment 2
#hibernate.c3p0.validate false





        在hibernate.cfg.xml文件里面加入如下配置:

<!-- 最大连接数 -->
        <property name="hibernate.c3p0.max_size">20</property>

        <!-- 最小连接数 -->
        <property name="hibernate.c3p0.min_size">5</property>

        <!-- 获得连接超时时间,如果超过这个时间,会抛出异常单位毫秒 -->
        <property name="hibernate.c3p0.timeout">120</property>

        <!-- 最大PreparedStatement数量 -->
        <property name="hibernate.c3p0.max_statements">100</property>

        <!-- 每隔120秒检查连接池里空闲连接 单位是秒-->
        <property name="hibernate.c3p0.idle_test_period">120</property>

        <!-- 当连接池里面连接用完时候C3P0下获取连接数 -->
        <property name="hibernate.c3p0.acquire_increment">2</property>

        <!-- 每次都验证连接是否可用 -->
        <property name="hibernate.c3p0.validate">true</property>






        完整举例如下(hibernate.properties):

hibernate.connection.driver_ = org.postgresql.Driver
hibernate.connection.url = jdbc:postgresql://localhost/mydatabase

hibernate.connection.username = myuser
hibernate.connection.password = secret
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=50
hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect






Tags:  hibernatec3p0 hibernate的连接池 hibernate连接池 c3p0连接池

延伸阅读

最新评论

发表评论