mysql No operations allowed after connection closed.Connection was implicitly closed due to underly

6094 ワード

原文の住所を転載する
このエラーを言う前に、私のプロジェクトがHibernateによってデータベース操作されたことを説明します.
MySQL接続のタイムアウトの問題については、多くの人が遭遇したことがあると思います.大体の場合、開発テストのプログラムは正常で、翌日になると何の間違いもありません.例えば、私のプロジェクトではタイミングタスクが実行され、毎日午前1時に実行されます.つまり、24時間おきに24時間実行され、8時間をはるかに超えています.
データベースのタイムアウトの最初の時間にログが表示された場合、最初のタイムアウトで発生したエラーは、ERROR[org.hibernate.util.JDBCExceptionReporter]-Communications link failureです.
Last packet sent to the server was 0 ms ago.
最初のタイムアウト後に実行されなければ、後でエラーを報告するたびにネストされたエラーになります.次のようにします.
ERROR [org.hibernate.util.JDBCExceptionReporter] -  No operations allowed after connection closed.Connection was implicitly closed due to underlying exception/error:
 
** BEGIN NESTED EXCEPTION **
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
MESSAGE: The last packet successfully received from the server was86395 milliseconds ago.The last packet sent successfully to the server was 86395 milliseconds ago, which  is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem. STACKTRACE:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was86395 milliseconds ago.The last packet sent successfully to the server was 86395 milliseconds ago, which  is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)  at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)  at java.lang.reflect.Constructor.newInstance(Unknown Source)  at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)  at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)  at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3270)  at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1932)  at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2101)  at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2554)  at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1761)  at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1912)  at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)  at org.hibernate.loader.Loader.getResultSet(Loader.java:1812)  at org.hibernate.loader.Loader.doQuery(Loader.java:697)  at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)  at org.hibernate.loader.Loader.doList(Loader.java:2232)  at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129)  at org.hibernate.loader.Loader.list(Loader.java:2124)  at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401)  at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)  at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)  at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1149)  at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)  at org.hibernate.impl.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:835)  at com.cn.util.db.TargetRecordDaoImpl.findbyIdAndDate(TargetRecordDaoImpl.java:23)  at com.cn.util.parser.ExcelOperate.readExcel(ExcelOperate.java:324)  at com.cn.util.parser.ExcelParser.parser(ExcelParser.java:41)  at com.cn.util.timer.CRMExcelParserTarger.execute(CRMExcelParserTarger.java:76)  at org.quartz.core.JobRunShell.run(JobRunShell.java:199)  at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:549) Caused by: java.net.SocketException: Software caused connection abort: socket write error  at java.net.SocketOutputStream.socketWrite0(Native Method)  at java.net.SocketOutputStream.socketWrite(Unknown Source)  at java.net.SocketOutputStream.write(Unknown Source)  at java.io.BufferedOutputStream.flushBuffer(Unknown Source)  at java.io.BufferedOutputStream.flush(Unknown Source)  at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3251)  ... 24 more
** END NESTED EXCEPTION **
具体的には、Mysqlサーバのデフォルトの「wait_timeout」は8時間【つまりデフォルトの値は28800秒】です.つまり、1つのconnectionの空きが8時間を超えると、Mysqlは自動的にこのconnectionを切断し、一般的には8時間以内に接続がアクティブではなく、自動的に接続を切断します.wait timeoutの値は設定できますが、最大2147483で、これ以上大きくはできません.つまり約24.85日だからMySQLがmyを通ってもini#TCP/IP Port the MySQL Server will listen on port=3306の下に#this is myown dinifition for mysql connection timeout wait_を追加timeout=31536000 interactive_timeout=31536000最大限の数値を超えても、MySQLで21474832147483に解析されるしかありません.その後、Hibernateの内蔵接続プールの性能が非常に悪いことがわかりました.第三者のc 3 p 0を直接使って、C 3 P 0接続プールに変更したほうがいいです.
この接続プールは、データベース接続が閉じられた場合に自動的に処理されます.C 3 P 0を使うのは簡単ですが、まずHibernateからc 3 p 0-0.9.1.JAr複
プロジェクトのlibディレクトリに作成し、hibernate.义齿c 3 p 0の先頭にある属性のコメント(使用不足)
値を節約したり、自分の必要な数値を節約したりすることで、Hibernateは内蔵の接続プールの代わりにC 3 P 0を自動的に使用します.これまでの問題
二度と現れたことがない.具体的にはhibernate.cfg.xmlでは次のように構成されています.
org.hibernate.connection.C3P0ConnectionProvider  
           
        5  
           
        30  
          
        1800  
           
        100  
           
        121  
           
        1  
        true