<default-config>
<!--褰撹繛鎺ユ睜涓殑榪炴帴鑰楀敖鐨勬椂鍊檆3p0涓嬈″悓鏃惰幏鍙栫殑榪炴帴鏁般侱efault: 3 -->
<property name="acquireIncrement">3</property>
<!--瀹氫箟鍦ㄤ粠鏁版嵁搴撹幏鍙栨柊榪炴帴澶辮觸鍚庨噸澶嶅皾璇曠殑嬈℃暟銆侱efault: 30 -->
<property name="acquireRetryAttempts">30</property>
<!--涓ゆ榪炴帴涓棿闅旀椂闂達紝鍗曚綅姣銆侱efault: 1000 -->
<property name="acquireRetryDelay">1000</property>
<!--榪炴帴鍏抽棴鏃墮粯璁ゅ皢鎵鏈夋湭鎻愪氦鐨勬搷浣滃洖婊氥侱efault: false -->
<property name="autoCommitOnClose">false</property>
<!--c3p0灝嗗緩涓寮犲悕涓篢est鐨勭┖琛紝騫朵嬌鐢ㄥ叾鑷甫鐨勬煡璇㈣鍙ヨ繘琛屾祴璇曘傚鏋滃畾涔変簡榪欎釜鍙傛暟閭d箞
灞炴referredTestQuery灝嗚蹇界暐銆備綘涓嶈兘鍦ㄨ繖寮燭est琛ㄤ笂榪涜浠諱綍鎿嶄綔錛屽畠灝嗗彧渚沜3p0嫻嬭瘯
浣跨敤銆侱efault: null-->
<property name="automaticTestTable">Test</property>
<!--鑾峰彇榪炴帴澶辮觸灝嗕細寮曡搗鎵鏈夌瓑寰呰繛鎺ユ睜鏉ヨ幏鍙栬繛鎺ョ殑綰跨▼鎶涘嚭寮傚父銆備絾鏄暟鎹簮浠嶆湁鏁?br /> 淇濈暀錛屽茍鍦ㄤ笅嬈¤皟鐢╣etConnection()鐨勬椂鍊欑戶緇皾璇曡幏鍙栬繛鎺ャ傚鏋滆涓簍rue錛岄偅涔堝湪灝濊瘯
鑾峰彇榪炴帴澶辮觸鍚庤鏁版嵁婧愬皢鐢蟲槑宸叉柇寮騫舵案涔呭叧闂侱efault: false-->
<property name="breakAfterAcquireFailure">false</property>
<!--褰撹繛鎺ユ睜鐢ㄥ畬鏃跺鎴風璋冪敤getConnection()鍚庣瓑寰呰幏鍙栨柊榪炴帴鐨勬椂闂達紝瓚呮椂鍚庡皢鎶涘嚭
SQLException,濡傝涓?鍒欐棤闄愭湡絳夊緟銆傚崟浣嶆縐掋侱efault: 0 -->
<property name="checkoutTimeout">100</property>
<!--閫氳繃瀹炵幇ConnectionTester鎴朡ueryConnectionTester鐨勭被鏉?/span> 嫻嬭瘯榪炴帴銆傜被鍚嶉渶鍒跺畾鍏ㄨ礬寰勩?br /> Default: com.mchange.v2.c3p0.impl.DefaultConnectionTester-->
<property name="connectionTesterClassName"></property>
<!--鎸囧畾c3p0 libraries鐨勮礬寰勶紝濡傛灉錛堥氬父閮芥槸榪欐牱錛夊湪鏈湴鍗沖彲鑾峰緱閭d箞鏃犻渶璁劇疆錛岄粯璁ull鍗沖彲
Default: null-->
<property name="factoryClassLocation">null</property>
<!--Strongly disrecommended. Setting this to true may lead to subtle and bizarre bugs.
錛堟枃妗e師鏂囷級浣滆呭己鐑堝緩璁笉浣跨敤鐨勪竴涓睘鎬?->
<property name="forceIgnoreUnresolvedTransactions">false</property>
<!--姣?0縐掓鏌ユ墍鏈夎繛鎺ユ睜涓殑絀洪棽榪炴帴銆侱efault: 0 -->
<property name="idleConnectionTestPeriod">60</property>
<!--鍒濆鍖栨椂鑾峰彇涓変釜榪炴帴錛屽彇鍊煎簲鍦╩inPoolSize涓巑axPoolSize涔嬮棿銆侱efault: 3 -->
<property name="initialPoolSize">3</property>
<!--鏈澶х┖闂叉椂闂?60縐掑唴鏈嬌鐢ㄥ垯榪炴帴琚涪寮冦傝嫢涓?鍒欐案涓嶄涪寮冦侱efault: 0 -->
<property name="maxIdleTime">60</property>
<!--榪炴帴姹犱腑淇濈暀鐨勬渶澶ц繛鎺ユ暟銆侱efault: 15 -->
<property name="maxPoolSize">15</property>
<!--JDBC鐨勬爣鍑嗗弬鏁幫紝鐢ㄤ互鎺у埗鏁版嵁婧愬唴鍔犺澆鐨凱reparedStatements鏁伴噺銆備絾鐢變簬棰勭紦瀛樼殑statements
灞炰簬鍗曚釜connection鑰屼笉鏄暣涓繛鎺ユ睜銆傛墍浠ヨ緗繖涓弬鏁伴渶瑕佽冭檻鍒板鏂歸潰鐨勫洜绱犮?br /> 濡傛灉maxStatements涓巑axStatementsPerConnection鍧囦負0錛屽垯緙撳瓨琚叧闂侱efault: 0-->
<property name="maxStatements">100</property>
<!--maxStatementsPerConnection瀹氫箟浜嗚繛鎺ユ睜鍐呭崟涓繛鎺ユ墍鎷ユ湁鐨勬渶澶х紦瀛榮tatements鏁般侱efault: 0 -->
<property name="maxStatementsPerConnection"></property>
<!--c3p0鏄紓姝ユ搷浣滅殑錛岀紦鎱㈢殑JDBC鎿嶄綔閫氳繃甯姪榪涚▼瀹屾垚銆傛墿灞曡繖浜涙搷浣滃彲浠ユ湁鏁堢殑鎻愬崌鎬ц兘
閫氳繃澶氱嚎紼嬪疄鐜板涓搷浣滃悓鏃惰鎵ц銆侱efault: 3-->
<property name="numHelperThreads">3</property>
<!--褰撶敤鎴瘋皟鐢╣etConnection()鏃朵嬌root鐢ㄦ埛鎴愪負鍘昏幏鍙栬繛鎺ョ殑鐢ㄦ埛銆備富瑕佺敤浜庤繛鎺ユ睜榪炴帴闈瀋3p0
鐨勬暟鎹簮鏃躲侱efault: null-->
<property name="overrideDefaultUser">root</property>
<!--涓巓verrideDefaultUser鍙傛暟瀵瑰簲浣跨敤鐨勪竴涓弬鏁般侱efault: null-->
<property name="overrideDefaultPassword">password</property>
<!--瀵嗙爜銆侱efault: null-->
<property name="password"></property>
<!--瀹氫箟鎵鏈夎繛鎺ユ祴璇曢兘鎵ц鐨勬祴璇曡鍙ャ傚湪浣跨敤榪炴帴嫻嬭瘯鐨勬儏鍐典笅榪欎釜涓鏄捐憲鎻愰珮嫻嬭瘯閫熷害銆傛敞鎰忥細
嫻嬭瘯鐨勮〃蹇呴』鍦ㄥ垵濮嬫暟鎹簮鐨勬椂鍊欏氨瀛樺湪銆侱efault: null-->
<property name="preferredTestQuery">select id from test where id=1</property>
<!--鐢ㄦ埛淇敼緋葷粺閰嶇疆鍙傛暟鎵ц鍓嶆渶澶氱瓑寰?00縐掋侱efault: 300 -->
<property name="propertyCycle">300</property>
<!--鍥犳ц兘娑堣楀ぇ璇峰彧鍦ㄩ渶瑕佺殑鏃跺欎嬌鐢ㄥ畠銆傚鏋滆涓簍rue閭d箞鍦ㄦ瘡涓猚onnection鎻愪氦鐨?br /> 鏃跺欓兘灝嗘牎楠屽叾鏈夋晥鎬с傚緩璁嬌鐢╥dleConnectionTestPeriod鎴朼utomaticTestTable
絳夋柟娉曟潵鎻愬崌榪炴帴嫻嬭瘯鐨勬ц兘銆侱efault: false -->
<property name="testConnectionOnCheckout">false</property>
<!--濡傛灉璁句負true閭d箞鍦ㄥ彇寰楄繛鎺ョ殑鍚屾椂灝嗘牎楠岃繛鎺ョ殑鏈夋晥鎬с侱efault: false -->
<property name="testConnectionOnCheckin">true</property>
<!--鐢ㄦ埛鍚嶃侱efault: null-->
<property name="user">root</property>
<!--鏃╂湡鐨刢3p0鐗堟湰瀵笿DBC鎺ュ彛閲囩敤鍔ㄦ佸弽灝勪唬鐞嗐傚湪鏃╂湡鐗堟湰鐢ㄩ斿箍娉涚殑鎯呭喌涓嬭繖涓弬鏁?br /> 鍏佽鐢ㄦ埛鎭㈠鍒板姩鎬佸弽灝勪唬鐞嗕互瑙e喅涓嶇ǔ瀹氱殑鏁呴殰銆傛渶鏂扮殑闈炲弽灝勪唬鐞嗘洿蹇茍涓斿凡緇忓紑濮?br /> 騫挎硾鐨勮浣跨敤錛屾墍浠ヨ繖涓弬鏁版湭蹇呮湁鐢ㄣ傜幇鍦ㄥ師鍏堢殑鍔ㄦ佸弽灝勪笌鏂扮殑闈炲弽灝勪唬鐞嗗悓鏃跺彈鍒?br /> 鏀寔錛屼絾浠婂悗鍙兘鐨勭増鏈彲鑳戒笉鏀寔鍔ㄦ佸弽灝勪唬鐞嗐侱efault: 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>
杞細http://www.wujianrong.com/archives/2007/08/c3p0.html
鏈榪戠殑涓涓」鐩湪Hibernate浣跨敤C3P0鐨勮繛鎺ユ睜錛屾暟鎹簱涓篗ysql銆傚紑鍙戞祴璇曟病鏈夐棶棰橈紝鍦ㄨ繍琛屼腑姣忎釜涓孌甸暱鐨勭┖闂叉椂闂村氨鍑虹幇寮傚父:
- org.hibernate.exception.JDBCConnectionException: could not execute query
- at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java: 74 )
- at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java: 43 )
- .......
- Caused by: com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: No operations allowed after connection closed.Connection was implicitly closed due to underlying exception/error:
- ** BEGIN NESTED EXCEPTION **
- com.mysql.jdbc.CommunicationsException
- MESSAGE: Communications link failure due to underlying exception:
- ** BEGIN NESTED EXCEPTION **
- java.net.SocketException
- MESSAGE: Broken pipe
- STACKTRACE:
- java.net.SocketException: Broken pipe
- at java.net.SocketOutputStream.socketWrite0(Native Method)
- ......
- ** END NESTED EXCEPTION **
鏌ョ湅浜哅ysql鐨勬枃妗o紝浠ュ強Connector/J鐨勬枃妗d互鍙婂湪綰胯鏄庡彂鐜幫紝鍑虹幇榪欑寮傚父鐨勫師鍥犳槸錛?/p>
Mysql鏈嶅姟鍣ㄩ粯璁ょ殑“wait_timeout”鏄?灝忔椂錛屼篃灝辨槸璇翠竴涓猚onnection絀洪棽瓚呰繃8涓皬鏃訛紝Mysql灝嗚嚜鍔ㄦ柇寮璇? connection銆傝繖灝辨槸闂鐨勬墍鍦紝鍦–3P0 pools涓殑connections濡傛灉絀洪棽瓚呰繃8灝忔椂錛孧ysql灝嗗叾鏂紑錛岃孋3P0騫朵笉鐭ラ亾璇onnection宸茬粡澶辨晥錛屽鏋滆繖鏃舵湁 Client璇鋒眰connection錛孋3P0灝嗚澶辨晥鐨凜onnection鎻愪緵緇機lient錛屽皢浼氶犳垚涓婇潰鐨勫紓甯搞?/p>
瑙e喅鐨勬柟娉曟湁3縐嶏細
- 澧炲姞wait_timeout鐨勬椂闂淬?
- 鍑忓皯Connection pools涓璫onnection鐨刲ifetime銆?
- 嫻嬭瘯Connection pools涓璫onnection鐨勬湁鏁堟с?
褰撶劧鏈濂界殑鍔炴硶鏄悓鏃剁患鍚堜嬌鐢ㄤ笂榪?縐嶆柟娉曪紝涓嬮潰灝盌BCP鍜孋3P0鍒嗗埆鍋氫竴璇存槑錛屽亣璁緒ait_timeout涓洪粯璁ょ殑8灝忔椂
DBCP澧炲姞浠ヤ笅閰嶇疆淇℃伅:
- //set to 'SELECT 1'
- validationQuery = "SELECT 1"
- //set to 'true'
- testWhileIdle = "true"
- //some positive integer
- timeBetweenEvictionRunsMillis = 3600000
- //set to something smaller than 'wait_timeout'
- minEvictableIdleTimeMillis = 18000000
- //if you don't mind a hit for every getConnection(), set to "true"
- testOnBorrow = "true"
C3P0澧炲姞浠ヤ笅閰嶇疆淇℃伅:
- //鑾峰彇connnection鏃舵祴璇曟槸鍚︽湁鏁?/span>
- testConnectionOnCheckin = true
- //鑷姩嫻嬭瘯鐨則able鍚嶇О
- automaticTestTable=C3P0TestTable
- //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
鏇村鐨勯厤緗俊鎭ぇ瀹跺彲浠ユ煡鐪婥3P0鏂囨。錛孋onnector/J鏂囨。錛屼互鍙奃BCP鐨勬枃妗c?/p>
杞? 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>
+++++++++++
鎶ラ敊璇細
APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks!
涓鑸緗甿axStatements=0瑙e喅璇ラ棶棰?
浣嗘槸錛?
鎶妋ax_statements璁劇疆涓?銆?
c3p0鍦ㄥ悓鏃跺叧闂璼tatement鍜宑onnection鐨勬椂鍊欙紝鎴栬呭叧闂粬浠箣闂寸殑鏃墮棿寰堢煭鐨勬椂鍊欙紝鏈夋椂鍊檆onnection騫舵病鏈夎鍏抽棴錛屽洜涓烘湁浜沺reparedstatement榪樺湪琚玞ached浣忋傝繖鏄痗3p0鐨勪綔鑰呰嚜宸辮鐨勩?
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浣滀負榪炴帴姹犳椂錛屽叾涓殑涓浜涢厤緗弬鏁伴渶瑕佷慨鏀廣備富瑕佹槸maxIdleTime鍜宨dleConnectionTestPeriod銆?
MySQL榛樿鏄?灝忔椂錛?8800縐掞級鍚庤嚜鍔ㄥ叧闂凡鎵撳紑鐨勮繛鎺ワ紝鎵浠3p0瑕佸湪8灝忔椂鍐呭叧闂笉浣跨敤鐨勮繛鎺ワ紝涓婇潰鐨?鍙傛暟瑕佸皬浜?8800縐掋傞檮涓婂湪
hibernate涓厤緗甤3p0鐨勫叧閿瓧銆?
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
杞澆鑷細http://handawei.javaeye.com/blog/651046