首先準備好須要的 jar 包,這裏我準備的是 Hibernate 5.2.10 版本:java
建立 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