com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.

來源:互聯網
上載者:User

標籤:串連   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.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.