hibernate使用c3p0資料來源,

來源:互聯網
上載者:User

hibernate使用c3p0資料來源,
在配置好hibernate串連資料庫環境的前提下,我們進行如下操作就可以搭建好hibernate中使用c3p0資料來源的環境了。1). 匯入 jar 包:
hibernate-release-4.2.4.Final\lib\optional\c3p0\*.jar(這裡面一般有3個jar包 )c3p0-0.9.2.1.jar
hibernate-c3p0-4.2.15.Final.jar
mchange-commons-java-0.2.3.4.jar

2). 加入配置(hibernate.cfg.xml檔案中添加):
hibernate.c3p0.max_size: 資料庫連接池的最大串連數hibernate.c3p0.min_size: 資料庫連接池的最小串連數hibernate.c3p0.acquire_increment: 當資料庫連接池中的串連耗盡時, 同一時刻擷取多少個資料庫連接hibernate.c3p0.timeout: 資料庫連接池中連線物件在多長時間沒有使用過後,就應該被銷毀hibernate.c3p0.idle_test_period: 表示串連池檢測線程多長時間檢測一次池內的所有連結化物件是否逾時.串連池本身不會把自己從串連池中移除,而是專門有一個線程按照一定的時間間隔來做這件事,這個線程通過比較連線物件最後一次被使用時間和目前時間的時間差來和 timeout 做對比,進而決定是否銷毀這個連線物件。hibernate.c3p0.max_statements: 緩衝 Statement 對象的數量
下面是加入配置的原版代碼:下面代碼添加在hibernate.cfg.xml檔案中

</pre><pre name="code" class="html"><!-- 配置c3p0資料來源 --><property name="hibernate.c3p0.max_size">10</property><property name="hibernate.c3p0.min_size">2</property><property name="c3p0.acquire_increment">2</property><property name="c3p0.idle_test_period">2000</property><property name="c3p0.timeout">2000</property><property name="c3p0.max_statements">10</property>

我們可以通過編寫測試案例來查看是否搭建成功:建立junit測試案例,在測試案例中複寫dowork方法來調用jdbc中的connection對象,然後列印出來
@Testpublic void testDoWork(){session.doWork(new Work() {@Overridepublic void execute(Connection connection) throws SQLException {System.out.println(connection); //調用預存程序. }});}

若列印了的出現了下面帶有c3p0的代碼就表示現在已經搭建成功。
com.mchange.v2.c3p0.impl.NewProxyConnection@5848ddac





相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.