隨筆-179  評(píng)論-666  文章-29  trackbacks-0
          阿里云服務(wù)器優(yōu)惠券
          <c3p0-config>
          <default-config>
          <!--當(dāng)連接池中的連接耗盡的時(shí)候c3p0一次同時(shí)獲取的連接數(shù)。Default: 3 -->
          <property name="acquireIncrement">3</property>

          <!--定義在從數(shù)據(jù)庫獲取新連接失敗后重復(fù)嘗試的次數(shù)。Default: 30 -->
          <property name="acquireRetryAttempts">30</property>

          <!--兩次連接中間隔時(shí)間,單位毫秒。Default: 1000 -->
          <property name="acquireRetryDelay">1000</property>

          <!--連接關(guān)閉時(shí)默認(rèn)將所有未提交的操作回滾。Default: false -->
          <property name="autoCommitOnClose">false</property>

          <!--c3p0將建一張名為Test的空表,并使用其自帶的查詢語句進(jìn)行測試。如果定義了這個(gè)參數(shù)那么
          屬性preferredTestQuery將被忽略。你不能在這張Test表上進(jìn)行任何操作,它將只供c3p0測試
          使用。Default: null-->
          <property name="automaticTestTable">Test</property>

          <!--獲取連接失敗將會(huì)引起所有等待連接池來獲取連接的線程拋出異常。但是數(shù)據(jù)源仍有效
          保留,并在下次調(diào)用getConnection()的時(shí)候繼續(xù)嘗試獲取連接。如果設(shè)為true,那么在嘗試
          獲取連接失敗后該數(shù)據(jù)源將申明已斷開并永久關(guān)閉。Default: false-->
          <property name="breakAfterAcquireFailure">false</property>

          <!--當(dāng)連接池用完時(shí)客戶端調(diào)用getConnection()后等待獲取新連接的時(shí)間,超時(shí)后將拋出
          SQLException,如設(shè)為0則無限期等待。單位毫秒。Default: 0 -->
          <property name="checkoutTimeout">100</property>

          <!--通過實(shí)現(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.
          (文檔原文)作者強(qiáng)烈建議不使用的一個(gè)屬性-->
          <property name="forceIgnoreUnresolvedTransactions">false</property>

          <!--每60秒檢查所有連接池中的空閑連接。Default: 0 -->
          <property name="idleConnectionTestPeriod">60</property>

          <!--初始化時(shí)獲取三個(gè)連接,取值應(yīng)在minPoolSize與maxPoolSize之間。Default: 3 -->
          <property name="initialPoolSize">3</property>

          <!--最大空閑時(shí)間,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
          屬于單個(gè)connection而不是整個(gè)連接池。所以設(shè)置這個(gè)參數(shù)需要考慮到多方面的因素。
          如果maxStatements與maxStatementsPerConnection均為0,則緩存被關(guān)閉。Default: 0-->
          <property name="maxStatements">100</property>

          <!--maxStatementsPerConnection定義了連接池內(nèi)單個(gè)連接所擁有的最大緩存statements數(shù)。Default: 0 -->
          <property name="maxStatementsPerConnection"></property>

          <!--c3p0是異步操作的,緩慢的JDBC操作通過幫助進(jìn)程完成。擴(kuò)展這些操作可以有效的提升性能
          通過多線程實(shí)現(xiàn)多個(gè)操作同時(shí)被執(zhí)行。Default: 3-->

          <property name="numHelperThreads">3</property>

          <!--當(dāng)用戶調(diào)用getConnection()時(shí)使root用戶成為去獲取連接的用戶。主要用于連接池連接非c3p0
          的數(shù)據(jù)源時(shí)。Default: null-->
          <property name="overrideDefaultUser">root</property>

          <!--與overrideDefaultUser參數(shù)對(duì)應(yīng)使用的一個(gè)參數(shù)。Default: null-->
          <property name="overrideDefaultPassword">password</property>

          <!--密碼。Default: null-->
          <property name="password"></property>

          <!--定義所有連接測試都執(zhí)行的測試語句。在使用連接測試的情況下這個(gè)一顯著提高測試速度。注意:
          測試的表必須在初始數(shù)據(jù)源的時(shí)候就存在。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>

          <!--因性能消耗大請(qǐng)只在需要的時(shí)候使用它。如果設(shè)為true那么在每個(gè)connection提交的
          時(shí)候都將校驗(yàn)其有效性。建議使用idleConnectionTestPeriod或automaticTestTable
          等方法來提升連接測試的性能。Default: false -->
          <property name="testConnectionOnCheckout">false</property>

          <!--如果設(shè)為true那么在取得連接的同時(shí)將校驗(yàn)連接的有效性。Default: false -->
          <property name="testConnectionOnCheckin">true</property>

          <!--用戶名。Default: null-->
          <property name="user">root</property>

          <!--早期的c3p0版本對(duì)JDBC接口采用動(dòng)態(tài)反射代理。在早期版本用途廣泛的情況下這個(gè)參數(shù)
          允許用戶恢復(fù)到動(dòng)態(tài)反射代理以解決不穩(wěn)定的故障。最新的非反射代理更快并且已經(jīng)開始
          廣泛的被使用,所以這個(gè)參數(shù)未必有用。現(xiàn)在原先的動(dòng)態(tài)反射與新的非反射代理同時(shí)受到
          支持,但今后可能的版本可能不支持動(dòng)態(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>


          轉(zhuǎn):http://www.wujianrong.com/archives/2007/08/c3p0.html
          解決MYSQL 8小時(shí)問題

          最近的一個(gè)項(xiàng)目在Hibernate使用C3P0的連接池,數(shù)據(jù)庫為Mysql。開發(fā)測試沒有問題,在運(yùn)行中每個(gè)一段長的空閑時(shí)間就出現(xiàn)異常:

          java 代碼
          1. org.hibernate.exception.JDBCConnectionException: could not execute query
          2. at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:74)
          3. at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
          4. .......
          5. Caused by: com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: No operations allowed after connection closed.Connection was implicitly closed due to underlying exception/error:
          6. ** BEGIN NESTED EXCEPTION **
          7. com.mysql.jdbc.CommunicationsException
          8. MESSAGE: Communications link failure due to underlying exception:
          9. ** BEGIN NESTED EXCEPTION **
          10. java.net.SocketException
          11. MESSAGE: Broken pipe
          12. STACKTRACE:
          13. java.net.SocketException: Broken pipe
          14. at java.net.SocketOutputStream.socketWrite0(Native Method)
          15. ......
          16. ** END NESTED EXCEPTION **

          查看了Mysql的文檔,以及Connector/J的文檔以及在線說明發(fā)現(xiàn),出現(xiàn)這種異常的原因是:

          Mysql服務(wù)器默認(rèn)的“wait_timeout”是8小時(shí),也就是說一個(gè)connection空閑超過8個(gè)小時(shí),Mysql將自動(dòng)斷開該 connection。這就是問題的所在,在C3P0 pools中的connections如果空閑超過8小時(shí),Mysql將其斷開,而C3P0并不知道該connection已經(jīng)失效,如果這時(shí)有 Client請(qǐng)求connection,C3P0將該失效的Connection提供給Client,將會(huì)造成上面的異常。

          解決的方法有3種:

          1. 增加wait_timeout的時(shí)間。
          2. 減少Connection pools中connection的lifetime。
          3. 測試Connection pools中connection的有效性。

          當(dāng)然最好的辦法是同時(shí)綜合使用上述3種方法,下面就DBCP和C3P0分別做一說明,假設(shè)wait_timeout為默認(rèn)的8小時(shí)

          DBCP增加以下配置信息:

          1. //set to 'SELECT 1'
          2. validationQuery = "SELECT 1"
          3. //set to 'true'
          4. testWhileIdle = "true"
          5. //some positive integer
          6. timeBetweenEvictionRunsMillis = 3600000
          7. //set to something smaller than 'wait_timeout'
          8. minEvictableIdleTimeMillis = 18000000
          9. //if you don't mind a hit for every getConnection(), set to "true"
          10. testOnBorrow = "true"

          C3P0增加以下配置信息:

          1. //獲取connnection時(shí)測試是否有效
          2. testConnectionOnCheckin = true
          3. //自動(dòng)測試的table名稱
          4. automaticTestTable=C3P0TestTable
          5. //set to something much less than wait_timeout, prevents connections from going stale
          6. idleConnectionTestPeriod = 18000
          7. //set to something slightly less than wait_timeout, preventing 'stale' connections from being handed out
          8. maxIdleTime = 25000
          9. //if you can take the performance 'hit', set to "true"
          10. testConnectionOnCheckout = true

          更多的配置信息大家可以查看C3P0文檔,Connector/J文檔,以及DBCP的文檔。

          轉(zhuǎn): http://www.javaeye.com/article/38506

          我自己的配置:

          jdbc.driverClass=com.mysql.jdbc.Driver
          jdbc.jdbcUrl = jdbc:mysql://localhost:3306/test
          jdbc.user = root
          jdbc.password = 12345
          jdbc.miniPoolSize = 1
          jdbc.maxPoolSize = 20
          jdbc.initialPoolSize = 1
          jdbc.maxIdleTime = 25000
          jdbc.acquireIncrement = 1

          jdbc.acquireRetryAttempts = 30
          jdbc.acquireRetryDelay = 1000
          jdbc.testConnectionOnCheckin = true
          jdbc.automaticTestTable = c3p0TestTable
          jdbc.idleConnectionTestPeriod = 18000
          jdbc.checkoutTimeout=3000

          <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
             <property name="driverClass" value="${jdbc.driverClass}" />
             <property name="jdbcUrl" value="${jdbc.jdbcUrl}" />
             <property name="user" value="${jdbc.user}" />
             <property name="password" value="${jdbc.password}" />
             <property name="minPoolSize" value="${jdbc.miniPoolSize}" />
             <property name="maxPoolSize" value="${jdbc.maxPoolSize}"/>  
             <property name="initialPoolSize" value="${jdbc.initialPoolSize}"/>
             <property name="maxIdleTime" value="${jdbc.maxIdleTime}"/>
             <property name="acquireIncrement" value="${jdbc.acquireIncrement}"/>
            
             <property name="acquireRetryAttempts" value="${jdbc.acquireRetryAttempts}"/>
             <property name="acquireRetryDelay" value="${jdbc.acquireRetryDelay}"/>
             <property name="testConnectionOnCheckin" value="${jdbc.testConnectionOnCheckin}"/>
             <property name="automaticTestTable" value="${jdbc.automaticTestTable}"/>
             <property name="idleConnectionTestPeriod" value="${jdbc.idleConnectionTestPeriod}"/>
             <property name="checkoutTimeout" value="${jdbc.checkoutTimeout}"/>

          </bean>

          阿里云服務(wù)器優(yōu)惠券
          posted on 2009-03-29 23:31 Alpha 閱讀(91821) 評(píng)論(26)  編輯  收藏 所屬分類: Hibernate

          評(píng)論:
          # re: c3p0詳細(xì)配置 2009-09-01 06:39 | changsir
          恩 很詳細(xì),我已經(jīng)收藏,感謝樓主。  回復(fù)  更多評(píng)論
            
          # re: c3p0詳細(xì)配置 2009-12-07 10:27 | 游客
          收藏,我一直以看帖要回為美德  回復(fù)  更多評(píng)論
            
          # re: c3p0詳細(xì)配置 2010-01-20 10:58 | 謝謝
          收藏了哈。。謝謝了  回復(fù)  更多評(píng)論
            
          # re: c3p0詳細(xì)配置 2010-01-29 11:11 | 武星
          # re: c3p0詳細(xì)配置[未登錄] 2010-03-04 22:47 | zy
          # re: c3p0詳細(xì)配置 2010-03-19 11:43 | 啊正
          十分感謝 很需要這樣的好文章  回復(fù)  更多評(píng)論
            
          # re: c3p0詳細(xì)配置 2010-06-22 20:27 | wdp
          精神可嘉  回復(fù)  更多評(píng)論
            
          # re: c3p0詳細(xì)配置 2010-11-22 15:51 | Aloong
          收藏了~很詳細(xì),謝謝樓主  回復(fù)  更多評(píng)論
            
          # re: c3p0詳細(xì)配置 2010-11-23 10:50 | hy
          真的很詳細(xì),收藏了  回復(fù)  更多評(píng)論
            
          # re: c3p0詳細(xì)配置[未登錄] 2011-02-23 16:48 | dongdong
          # re: c3p0詳細(xì)配置 2011-03-09 16:51 | ast
          很詳細(xì) 很好  回復(fù)  更多評(píng)論
            
          # re: c3p0詳細(xì)配置 2011-04-07 08:56 | longaohun
          收藏了~很詳細(xì),謝謝樓主  回復(fù)  更多評(píng)論
            
          # re: c3p0詳細(xì)配置[未登錄] 2011-05-06 17:07 | oo
          寫的不錯(cuò)...  回復(fù)  更多評(píng)論
            
          # re: c3p0詳細(xì)配置 2011-08-10 09:05 | 拿破侖正方形
          謝了,長見識(shí)了  回復(fù)  更多評(píng)論
            
          # re: c3p0詳細(xì)配置 2012-05-09 09:49 | ffff
          @啊正
          afdsafdsa  回復(fù)  更多評(píng)論
            
          # re: c3p0詳細(xì)配置 2012-09-03 18:53 | YJ
          樓主,弱弱的問一句,什么是C3P0  回復(fù)  更多評(píng)論
            
          # re: c3p0詳細(xì)配置[未登錄] 2013-06-21 16:24 |
          謝謝樓主,收藏之  回復(fù)  更多評(píng)論
            
          # re: c3p0詳細(xì)配置[未登錄] 2014-04-11 16:20 | 小浩
          相當(dāng)好~~~~  回復(fù)  更多評(píng)論
            
          # re: c3p0詳細(xì)配置[未登錄] 2014-06-16 15:03 | eric
          非常好!   回復(fù)  更多評(píng)論
            
          # re: c3p0詳細(xì)配置[未登錄] 2014-10-31 13:58 | li
          # re: c3p0詳細(xì)配置 2014-12-31 10:14 | ibugs
          不錯(cuò)!!
            回復(fù)  更多評(píng)論
            
          # re: c3p0詳細(xì)配置 2015-01-22 10:39 | 孫行者
          不錯(cuò),看帖必回啊,尤其是無私奉獻(xiàn)的我們  回復(fù)  更多評(píng)論
            
          # re: c3p0詳細(xì)配置 2015-02-03 15:28 | 333
          講的很詳細(xì) 謝lz了  回復(fù)  更多評(píng)論
            
          # re: c3p0詳細(xì)配置 2015-03-06 18:32 | 流云隨風(fēng)
          很好,感謝樓主!  回復(fù)  更多評(píng)論
            
          # re: c3p0詳細(xì)配置 2015-03-09 16:24 | 二師兄
          感謝樓主,已收藏。  回復(fù)  更多評(píng)論
            
          # re: c3p0詳細(xì)配置 2015-09-21 15:25 | hakil
          非常感謝...解決了C3PO 8小時(shí)斷開的問題。  回復(fù)  更多評(píng)論
            

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 正安县| 郴州市| 宜良县| 孝义市| 藁城市| 威信县| 响水县| 万盛区| 巴里| 金湖县| 乡城县| 白河县| 沽源县| 台山市| 鹤峰县| 五华县| 江达县| 文山县| 千阳县| 渭南市| 茂名市| 九龙坡区| 三穗县| 临湘市| 淮阳县| 扎赉特旗| 湘西| 昌吉市| 卢氏县| 大石桥市| 灵武市| 武平县| 伊吾县| 奉新县| 苗栗市| 章丘市| 冷水江市| 南投市| 梅河口市| 茂名市| 中山市|