C3P0 配置

官方文檔 : http://www.mchange.com/projects/c3p0/index.htmlhtml

3 java

30 mysql

1000 sql

false 數據庫

Test 緩存

false 服務器

100 多線程

null less

false 異步

60

3

60

15

100

3

root

password

select id from test where id=1

300

false

true

root

false

con_test

30000

30

10

30

25

10

0

200

300

轉:http://www.wujianrong.com/archives/2007/08/c3p0.html

解決MYSQL 8小時問題

最近的一個項目在Hibernate使用C3P0的鏈接池,數據庫爲Mysql。開發測試沒有問題,在運行中每一個一段長的空閒時間就出現異常:

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的文檔以及在線說明發現,出現這種異常的緣由是:

Mysql服務器默認的「wait_timeout」是8小時,也就是說一個connection空閒超過8個小時,Mysql將自動斷開該 connection。這就是問題的所在,在C3P0 pools中的connections若是空閒超過8小時,Mysql將其斷開,而C3P0並不知道該connection已經失效,若是這時有 Client請求connection,C3P0將該失效的Connection提供給Client,將會形成上面的異常。

解決的方法有3種:

  1. 增長wait_timeout的時間。
  2. 減小Connection pools中connection的lifetime。
  3. 測試Connection pools中connection的有效性。

 

固然最好的辦法是同時綜合使用上述3種方法,下面就DBCP和C3P0分別作一說明,假設wait_timeout爲默認的8小時

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時測試是否有效
  2. testConnectionOnCheckin = true
  3. //自動測試的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的文檔。

轉: 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

相關文章
相關標籤/搜索