您的位置:首页 > 其它

hibernate配置c3p0连接池

2014-01-17 17:52 555 查看
Hibernate自带的连接池算法相当不成熟。 它只是为了让你快些上手,并不适合用于产品系统或性能测试中。 出于最佳性能和稳定性考虑你应该使用第三方的连接池。只需要用特定连接池的设置替换 hibernate.connection.pool_size即可。这将关闭Hibernate自带的连接池。 例如, 你可能会想用C3P0.

  C3P0是一个随Hibernate一同分发的开源的JDBC连接池, 它位于lib目录下。 如果你设置了hibernate.c3p0.*相关的属性, Hibernate将使用 C3P0ConnectionProvider来缓存JDBC连接。 如果你更原意使用Proxool, 请参考发 行包中的hibernate.properties并到Hibernate网站获取更多的信息。

  <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>

                <prop key="hibernate.connection.url">jdbc:mysql://localhost:3306/portal?useUnicode=true&characterEncoding=UTF-8</prop>

                <prop key="hibernate.connection.username">root</prop>

                <prop key="hibernate.connection.password">root</prop>

                <prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>

                <!-- 最小连接数 -->

                <prop key="hibernate.c3p0.min_size">5</prop>

                <!-- 最大连接数 -->

                <prop key="hibernate.c3p0.max_size">20</prop>

                <!-- 获得连接的超时时间,如果超过这个时间,会抛出异常,单位毫秒 -->

                <prop key="hibernate.c3p0.timeout">500</prop>

                <!-- 最大的PreparedStatement的数量 -->

                <prop key="hibernate.c3p0.max_statements">100</prop>

                <!-- 每隔120秒检查连接池里的空闲连接 ,单位是秒-->

                <prop key="hibernate.c3p0.idle_test_period">120</prop>

                <!-- 当连接池里面的连接用完的时候,C3P0一下获取的新的连接数 -->

                <prop key="hibernate.c3p0.acquire_increment">3</prop>

                <!-- 每次都验证连接是否可用 -->

                <prop key="hibernate.c3p0.validate">true</prop>

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

  hibernate.connection.driver_class = 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

配置的时候需要加上

<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>

不然c3p0的配置不起作用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Hibernate c3p0