From :http://www.codeweblog.com/hibernate-using-c3p0-connection-pooling/web
c3p0是開源JDBC鏈接池,Hibernate的發佈版也有此功能。這篇文章描述怎樣使用Hibernate來配置從c3p0。C3p0鏈接池配置很簡單,只須要添加以下內容到 hibernate.cfg.xml文件:(原文:c3p0 for open source's JDBC connection pool, with the release hibernate. This article describes how to use the hibernate configuration in c3p0. c3p0 connection pool configuration is very simple, only needs to increase in hibernate.cfg.xml: )數據庫
<property name="hibernate.c3p0.max_size"> 20 </ property> <! - The smallest number of connections -> <property name="hibernate.c3p0.min_size"> 5 </ property> <! - Get connection timeout, if more than this time will be thrown, unit ms -> <property name="hibernate.c3p0.timeout"> 120 </ property> <! - The largest number of PreparedStatement -> <property name="hibernate.c3p0.max_statements"> 100 </ property> <! - Every 120 seconds to connect the pond to check the idle connection, units are seconds -> <property name="hibernate.c3p0.idle_test_period"> 120 </ property> <! - When the connection inside the connection pool run out of time, C3P0 click access to the new connections -> <property name="hibernate.c3p0.acquire_increment"> 2 </ property> <! - Every time, verify the connection is available -> <property name="hibernate.c3p0.validate"> true </ property>
此外,須要在CLASS_PATH中添加c3p0-xxx.jar文件,c3p0-xxx.jar的文檔發佈在Hibernate的conjunction下,你能夠在它的lib目錄下找到文件。(原文:In addition, where necessary in CLASS_PATH add c3p0-xxxjar file (xxx is version number), c3p0-xxxjar document released in conjunction with hibernate, you may, in its lib directory to find the file. )緩存
配置完成以後,Hibernate將能夠動態地使用c3p0鏈接池: C3P0ConnectionProvider(原文:After configuration, hibernate will automatically use the c3p0 connection pool: C3P0ConnectionProvider)
多線程
另附Hibernate c3p0的詳細配置,摘自其餘網站內容:異步
<c3p0-config> <default-config> <!--當鏈接池中的鏈接耗盡的時候c3p0一次同時獲取的鏈接數。Default: 3 --> <property name="acquireIncrement">3</property> <!--定義在從數據庫獲取新鏈接失敗後重復嘗試的次數。Default: 30 --> <property name="acquireRetryAttempts">30</property> <!--兩次鏈接中間隔時間,單位毫秒。Default: 1000 --> <property name="acquireRetryDelay">1000</property> <!--鏈接關閉時默認將全部未提交的操做回滾。Default: false --> <property name="autoCommitOnClose">false</property> <!--c3p0將建一張名爲Test的空表,並使用其自帶的查詢語句進行測試。若是定義了這個參數那麼 屬性preferredTestQuery將被忽略。你不能在這張Test表上進行任何操做,它將只供c3p0測試 使用。Default: null--> <property name="automaticTestTable">Test</property> <!--獲取鏈接失敗將會引發全部等待鏈接池來獲取鏈接的線程拋出異常。可是數據源仍有效 保留,並在下次調用getConnection()的時候繼續嘗試獲取鏈接。若是設爲true,那麼在嘗試 獲取鏈接失敗後該數據源將申明已斷開並永久關閉。Default: false--> <property name="breakAfterAcquireFailure">false</property> <!--當鏈接池用完時客戶端調用getConnection()後等待獲取新鏈接的時間,超時後將拋出 SQLException,如設爲0則無限期等待。單位毫秒。Default: 0 --> <property name="checkoutTimeout">100</property> <!--經過實現ConnectionTester或QueryConnectionTester的類來測試鏈接。類名需制定全路徑。 Default: com.mchange.v2.c3p0.impl.DefaultConnectionTester--> <property name="connectionTesterClassName"></property> <!--指定c3p0 libraries的路徑,若是(一般都是這樣)在本地便可得到那麼無需設置,默認null便可 Default: null--> <property name="factoryClassLocation">null</property> <!--Strongly disrecommended. Setting this to true may lead to subtle and bizarre bugs. (文檔原文)做者強烈建議不使用的一個屬性--> <property name="forceIgnoreUnresolvedTransactions">false</property> <!--每60秒檢查全部鏈接池中的空閒鏈接。Default: 0 --> <property name="idleConnectionTestPeriod">60</property> <!--初始化時獲取三個鏈接,取值應在minPoolSize與maxPoolSize之間。Default: 3 --> <property name="initialPoolSize">3</property> <!--最大空閒時間,60秒內未使用則鏈接被丟棄。若爲0則永不丟棄。Default: 0 --> <property name="maxIdleTime">60</property> <!--鏈接池中保留的最大鏈接數。Default: 15 --> <property name="maxPoolSize">15</property> <!--JDBC的標準參數,用以控制數據源內加載的PreparedStatements數量。但因爲預緩存的statements 屬於單個connection而不是整個鏈接池。因此設置這個參數須要考慮到多方面的因素。 若是maxStatements與maxStatementsPerConnection均爲0,則緩存被關閉。Default: 0--> <property name="maxStatements">100</property> <!--maxStatementsPerConnection定義了鏈接池內單個鏈接所擁有的最大緩存statements數。Default: 0 --> <property name="maxStatementsPerConnection"></property> <!--c3p0是異步操做的,緩慢的JDBC操做經過幫助進程完成。擴展這些操做能夠有效的提高性能 經過多線程實現多個操做同時被執行。Default: 3--> <property name="numHelperThreads">3</property> <!--當用戶調用getConnection()時使root用戶成爲去獲取鏈接的用戶。主要用於鏈接池鏈接非c3p0 的數據源時。Default: null--> <property name="overrideDefaultUser">root</property> <!--與overrideDefaultUser參數對應使用的一個參數。Default: null--> <property name="overrideDefaultPassword">password</property> <!--密碼。Default: null--> <property name="password"></property> <!--定義全部鏈接測試都執行的測試語句。在使用鏈接測試的狀況下這個一顯著提升測試速度。注意: 測試的表必須在初始數據源的時候就存在。Default: null--> <property name="preferredTestQuery">select id from test where id=1</property> <!--用戶修改系統配置參數執行前最多等待300秒。Default: 300 --> <property name="propertyCycle">300</property> <!--因性能消耗大請只在須要的時候使用它。若是設爲true那麼在每一個connection提交的 時候都將校驗其有效性。建議使用idleConnectionTestPeriod或automaticTestTable 等方法來提高鏈接測試的性能。Default: false --> <property name="testConnectionOnCheckout">false</property> <!--若是設爲true那麼在取得鏈接的同時將校驗鏈接的有效性。Default: false --> <property name="testConnectionOnCheckin">true</property> <!--用戶名。Default: null--> <property name="user">root</property> <!--早期的c3p0版本對JDBC接口採用動態反射代理。在早期版本用途普遍的狀況下這個參數 容許用戶恢復到動態反射代理以解決不穩定的故障。最新的非反射代理更快而且已經開始 普遍的被使用,因此這個參數未必有用。如今原先的動態反射與新的非反射代理同時受到 支持,但從此可能的版本可能不支持動態反射代理。Default: false--> <property name="usesTraditionalReflectiveProxies">false</property> <property name="automaticTestTable">con_test</property> <property name="checkoutTimeout">30000</property> <property name="idleConnectionTestPeriod">30</property> <property name="initialPoolSize">10</property> <property name="maxIdleTime">30</property> <property name="maxPoolSize">25</property> <property name="minPoolSize">10</property> <property name="maxStatements">0</property> <user-overrides user="swaldman"> </user-overrides> </default-config> <named-config name="dumbTestConfig"> <property name="maxStatements">200</property> <user-overrides user="poop"> <property name="maxStatements">300</property> </user-overrides> </named-config> </c3p0-config>