Hibernate 5.x 配置 C3P0 數據庫鏈接池

Hibernate 5.x 配置 C3P0 鏈接池

首先準備好須要的 jar 包,這裏我準備的是 Hibernate 5.2.10 版本:java

  • hibernate-core-5.2.10.Final.jar
  • hibernate-c3p0-5.2.10.Final.jar
  • c3p0-0.9.5.2.jar
  • mchange-commons-java-0.2.11.jar

建立 hibernate.cfg.xml 配置文件,針對 C3P0 的配置代碼以下:數據庫

<?xml version='1.0' encoding='UTF-8'?>  
<!DOCTYPE hibernate-configuration PUBLIC  
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
          "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">  
<hibernate-configuration>
    <session-factory>
        <!--C3P0配置 -->
        <property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property>
        <property name="hibernate.c3p0.max_size">20</property>
        <property name="hibernate.c3p0.min_size">5</property>
        <property name="hibernate.c3p0.timeout">120</property>
        <property name="automaticTestTable">Test</property>
        <property name="hibernate.c3p0.max_statements">100</property>
        <property name="hibernate.c3p0.idle_test_period">120</property>
        <property name="hibernate.c3p0.acquire_increment">1</property>
        <property name="c3p0.testConnectionOnCheckout">true</property>
        <property name="c3p0.idleConnectionTestPeriod">18000</property>
        <property name="c3p0.maxIdleTime">25000</property>
        <property name="c3p0.idle_test_period">120</property>
        
        <!-- ... 其它省略 -->

    </session-factory>
</hibernate-configuration>

hibernate.cfg.xml 配置完還沒結束,咱們能夠啓動 hibernate 看看控制檯的日誌輸出,通常開發環境中感受不到 C3P0 的優點做用,但只要 hibernate 能加載 C3P0 ,而且可以正常訪問操做數據庫,就說明 C3P0 配置成功了。session

相關文章
相關標籤/搜索