Hibernate part 15:C3P0连接池配置

Hibernate有默认的连接池,是DriverManagerConnectionProvider

12:51:52,892  INFO DriverManagerConnectionProvider:64 - Using Hibernate built-in connection pool (not for production use!)
12:51:52,894  INFO DriverManagerConnectionProvider:65 - Hibernate connection pool size: 20
12:51:52,894  INFO DriverManagerConnectionProvider:68 - autocommit mode: false
12:51:52,900  INFO DriverManagerConnectionProvider:103 - using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql:///test
12:51:52,901  INFO DriverManagerConnectionProvider:109 - connection properties: {user=root, password=****}

 导入jar包, hibernate解压目录/lib/optional/c3p0/c3p0-0.9.1.jar

修改hibernate.cfg.xml配置文件,增加连接池的配置

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

控制台输出

13:07:36,405  INFO ConnectionProviderFactory:173 - Initializing connection provider: org.hibernate.connection.C3P0ConnectionProvider
13:07:36,408  INFO C3P0ConnectionProvider:103 - C3P0 using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql:///test
13:07:36,408  INFO C3P0ConnectionProvider:104 - Connection properties: {user=root, password=****}
13:07:36,408  INFO C3P0ConnectionProvider:107 - autocommit mode: false

 C3P0的详细配置

<!-- C3P0连接池设定-->
<!-- 使用c3po连接池  配置连接池提供的供应商-->
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider                                                                                                                                                     </property>
<!--在连接池中可用的数据库连接的最少数目 -->
<property name="c3p0.min_size">5</property>
<!--在连接池中所有数据库连接的最大数目  -->
<property name="c3p0.max_size">20</property>
<!--设定数据库连接的过期时间,以秒为单位,
如果连接池中的某个数据库连接处于空闲状态的时间超过了timeout时间,就会从连接池中清除 -->
<property name="c3p0.timeout">120</property>
 <!--每3000秒检查所有连接池中的空闲连接 以秒为单位-->
<property name="c3p0.idle_test_period">3000</property>
<!-- 设置自动提交 -->
<property name="connection.autocommit">true</property>

相关推荐