c3p0的配置解释
找到了一個關(guān)于c3p0的配置?
<c3p0-config>?
<default-config>?
<!--當(dāng)連接池中的連接耗盡的時候c3p0一次同時獲取的連接數(shù)。Default: 3 -->?
<property name="acquireIncrement">3</property>?
<!--定義在從數(shù)據(jù)庫獲取新連接失敗后重復(fù)嘗試的次數(shù)。Default: 30 -->?
<property name="acquireRetryAttempts">30</property>?
<!--兩次連接中間隔時間,單位毫秒。Default: 1000 -->?
<property name="acquireRetryDelay">1000</property>?
<!--連接關(guān)閉時默認(rèn)將所有未提交的操作回滾。Default: false -->?
<property name="autoCommitOnClose">false</property>?
<!--c3p0將建一張名為Test的空表,并使用其自帶的查詢語句進(jìn)行測試。如果定義了這個參數(shù)那么?
屬性preferredTestQuery將被忽略。你不能在這張Test表上進(jìn)行任何操作,它將只供c3p0測試?
使用。Default: null-->?
<property name="automaticTestTable">Test</property>?
<!--獲取連接失敗將會引起所有等待連接池來獲取連接的線程拋出異常。但是數(shù)據(jù)源仍有效?
保留,并在下次調(diào)用getConnection()的時候繼續(xù)嘗試獲取連接。如果設(shè)為true,那么在嘗試?
獲取連接失敗后該數(shù)據(jù)源將申明已斷開并永久關(guān)閉。Default: false-->?
<property name="breakAfterAcquireFailure">false</property>?
<!--當(dāng)連接池用完時客戶端調(diào)用getConnection()后等待獲取新連接的時間,超時后將拋出?
SQLException,如設(shè)為0則無限期等待。單位毫秒。Default:?0?-->?
<property name="checkoutTimeout">100</property>?
<!--通過實現(xiàn)ConnectionTester或QueryConnectionTester的類來測試連接。類名需制定全路徑。?
Default: com.mchange.v2.c3p0.impl.DefaultConnectionTester-->?
<property name="connectionTesterClassName"></property>?
<!--指定c3p0 libraries的路徑,如果(通常都是這樣)在本地即可獲得那么無需設(shè)置,默認(rèn)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>?
<!--初始化時獲取三個連接,取值應(yīng)在minPoolSize與maxPoolSize之間。Default: 3 -->?
<property name="initialPoolSize">3</property>?
<!--最大空閑時間,60秒內(nèi)未使用則連接被丟棄。若為0則永不丟棄。Default:?0?-->?
<property name="maxIdleTime">60</property>?
<!--連接池中保留的最大連接數(shù)。Default: 15 -->?
<property name="maxPoolSize">15</property>?
<!--JDBC的標(biāo)準(zhǔn)參數(shù),用以控制數(shù)據(jù)源內(nèi)加載的PreparedStatements數(shù)量。但由于預(yù)緩存的statements?
屬于單個connection而不是整個連接池。所以設(shè)置這個參數(shù)需要考慮到多方面的因素。?
如果maxStatements與maxStatementsPerConnection均為0,則緩存被關(guān)閉。Default:?0-->?
<property name="maxStatements">100</property>?
<!--maxStatementsPerConnection定義了連接池內(nèi)單個連接所擁有的最大緩存statements數(shù)。Default:?0?-->?
<property name="maxStatementsPerConnection"></property>?
<!--c3p0是異步操作的,緩慢的JDBC操作通過幫助進(jìn)程完成。擴展這些操作可以有效的提升性能?
通過多線程實現(xiàn)多個操作同時被執(zhí)行。Default: 3-->?
<property name="numHelperThreads">3</property>?
<!--當(dāng)用戶調(diào)用getConnection()時使root用戶成為去獲取連接的用戶。主要用于連接池連接非c3p0?
的數(shù)據(jù)源時。Default: null-->?
<property name="overrideDefaultUser">root</property>?
<!--與overrideDefaultUser參數(shù)對應(yīng)使用的一個參數(shù)。Default: null-->?
<property name="overrideDefaultPassword">password</property>?
<!--密碼。Default: null-->?
<property name="password"></property>?
<!--定義所有連接測試都執(zhí)行的測試語句。在使用連接測試的情況下這個一顯著提高測試速度。注意:?
測試的表必須在初始數(shù)據(jù)源的時候就存在。Default: null-->?
<property name="preferredTestQuery">select id from test where id=1</property>?
<!--用戶修改系統(tǒng)配置參數(shù)執(zhí)行前最多等待300秒。Default: 300 -->?
<property name="propertyCycle">300</property>?
<!--因性能消耗大請只在需要的時候使用它。如果設(shè)為true那么在每個connection提交的?
時候都將校驗其有效性。建議使用idleConnectionTestPeriod或automaticTestTable?
等方法來提升連接測試的性能。Default: false -->?
<property name="testConnectionOnCheckout">false</property>?
<!--如果設(shè)為true那么在取得連接的同時將校驗連接的有效性。Default: false -->?
<property name="testConnectionOnCheckin">true</property>?
<!--用戶名。Default: null-->?
<property name="user">root</property>?
<!--早期的c3p0版本對JDBC接口采用動態(tài)反射代理。在早期版本用途廣泛的情況下這個參數(shù)?
允許用戶恢復(fù)到動態(tài)反射代理以解決不穩(wěn)定的故障。最新的非反射代理更快并且已經(jīng)開始?
廣泛的被使用,所以這個參數(shù)未必有用。現(xiàn)在原先的動態(tài)反射與新的非反射代理同時受到?
支持,但今后可能的版本可能不支持動態(tài)反射代理。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>?
=====================?
報錯誤:?
APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks!?
一般設(shè)置maxStatements=0解決該問題?
但是:?
把max_statements設(shè)置為0。?
c3p0在同時關(guān)閉statement和connection的時候,或者關(guān)閉他們之間的時間很短的時候,有時候connection并沒有被關(guān)閉,因為有些preparedstatement還在被cached住。這是c3p0的作者自己說的。?
http://forum.hibernate.org/viewtopic.php?t=947246&highlight=apparent+deadlock+c3p0?
C3P0增加以下配置信息:?
//set to 'SELECT 1'???????
preferredTestQuery = 'SELECT 1'?????
//set to something much less than wait_timeout, prevents connections from going stale????
idleConnectionTestPeriod?= 18000??????
//set to something slightly less than wait_timeout, preventing 'stale' connections from being handed out????
maxIdleTime = 25000?????
//if you can take the performance 'hit', set to "true"????
testConnectionOnCheckout?= true?????
A c3p0 pool with the settings you have should recover from a database reset, but that doesn't mean you will never see an Exception. Stale Connections from the old database session will still be broken, and if those Connections have already been checked out, or if they are in the pool and not tested on checkout, the application will see the broken Connection, in the form of an Exception.?
You can use c3p0 to minimize the likelihood that your application will see a stale Connection on database shutdown/restart. The most reliable means of preventing this is to set hibernate.c3p0.validate to true (in a hibernate application -- all other c3p0 apps should use the c3p0-native property c3p0.testConnectionOnCheckout). If you set this property to true, c3p0 will test Connections prior to checkout, and your app will never see a stale Connection on database restart unless the Connection had already been checked out when the database went down.?
Another less reliable, but potentially less expensive, strategy is to set c3p0.testConnectionsOnCheckin?and hibernate.c3p0.idle_test_period (c3p0-native c3p0.idleConnectionTestPeriod) to a low value, in which case all connection tests are asynchronous and you are guanteed that no Connection will be checked out that hasn't been tested in the last idle_test_period seconds. Thus, your app will only see broken Connections from the pool if Connections are checked out during a short window of time.?
In either case, I recommend setting "c3p0.preferredTestQuery" or "c3p0.automaticTestTable" in your c3p0 properties file, as c3p0's default Connection test is often slow.?
See "Configuring Connection Testing" in c3p0's docs for more information.?
在使用c3p0作為連接池時,其中的一些配置參數(shù)需要修改。主要是maxIdleTime和idleConnectionTestPeriod。 MySQL默認(rèn)是8小時(28800秒)后自動關(guān)閉已打開的連接,所以c3p0要在8小時內(nèi)關(guān)閉不使用的連接,上面的2參數(shù)要小于28800秒。附上在 hibernate中配置c3p0的關(guān)鍵字。?
c3p0-native property name hibernate configuration key?
c3p0.acquireIncrement hibernate.c3p0.acquire_increment?
c3p0.idleConnectionTestPeriod?hibernate.c3p0.idle_test_period?
c3p0.initialPoolSize not available -- uses minimum size?
c3p0.maxIdleTime hibernate.c3p0.timeout?
c3p0.maxPoolSize hibernate.c3p0.max_size?
c3p0.maxStatements hibernate.c3p0.max_statements?
c3p0.minPoolSize hibernate.c3p0.min_size
總結(jié)
- 上一篇: hibernate.jdbc.fetch
- 下一篇: 贵州贵阳汉堡炸鸡学校门口可以卖的那种在哪