標籤:串連 orm ntc sla 持久 nec 失效 erro val
ERROR - No operations allowed after connection closed. org.hibernate.exception.JDBCConnectionException: could not execute queryat org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:74)at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)at org.hibernate.loader.Loader.doList(Loader.java:2148)at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)at org.hibernate.loader.Loader.list(Loader.java:2024)at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:375)at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:308)at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:153)at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1106)at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)分析,出現這種異常的原因是:Mysql 伺服器預設的“wait_timeout”是8小時,也就是說一個connection空閑超過8個小時,Mysql將自動斷開該connection。 這就是問題的所在,在C3P0 pools中的connections如果空閑超過8小時,mysql將其斷開,而C3P0並不知道該connection已經失效,如果這時有 Client請求connection,C3P0將該失效的Connection提供給Client,將會造成上面的異常。a)解決的方法有3種: 增加wait_timeout的時間。 減少Connection pools中connection的lifetime。 測試Connection pools中connection的有效性。b)具體解決方案:
1、換一下JDBC驅動,JDBC3.1.0-alpha及以前版本會出現此問題,下載新的JDBC驅動 2、使用hibernate配置:使用hibernate: <property name="connection.autoReconnect">true</property> <!--這個是最主要的--> <property name="connection.autoReconnectForPools">true</property> <property name="connection.is-connection-validation-required">true</property> 加入以上property,可解決此問題,如果未使用hibernate等持久化管理架構,可在mysql的url中加入autoReconnect=true,這樣就可以解決。原 因很簡單。在對資料庫操作結束後關閉串連是正確的做法,沒什麼大問題。至於出現:No operations allowed after connection closed。這樣的問題原因只有一個,你這裡和資料庫的串連Connection是一個Static的,程式共用這一個Connection。所以第一 次對資料庫操作沒問題,當把Connection關閉後,第二次還想操作資料庫時Connection肯定不存在了。
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.