本文主要是介绍discard connection,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
现象
2021/05/31-19:36:38.927 [] [ShardingSphere-0] ERROR com.alibaba.druid.pool.DruidDataSource- discard connection
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after statement closed.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1015)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:975)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920)
at com.mysql.jdbc.StatementImpl.checkClosed(StatementImpl.java:461)
at com.mysql.jdbc.PreparedStatement.execute$original$GSeftvfb(PreparedStatement.java:1274)
at com.mysql.jdbc.PreparedStatement.execute$original$GSeftvfb$accessor$6BywaSxn(PreparedStatement.java)
at com.mysql.jdbc.PreparedStatement$auxiliary$eBJ6JSqZ.call(Unknown Source)
at com.jd.pfinder.profiler.tracer.plugin.invoker.InstanceEnhanceInvoker.intercept(InstanceEnhanceInvoker.java:62)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java)
at com.alibaba.druid.pool.DruidPooledPreparedStatement.execute(DruidPooledPreparedStatement.java:497)
at org.apache.shardingsphere.shardingjdbc.executor.SQLExecuteCallbackFactory$2.executeSQL(SQLExecuteCallbackFactory.java:62)
at org.apache.shardingsphere.shardingjdbc.executor.SQLExecuteCallbackFactory$2.executeSQL(SQLExecuteCallbackFactory.java:58)
at org.apache.shardingsphere.sharding.execute.sql.execute.SQLExecuteCallback.execute0(SQLExecuteCallback.java:82)
at org.apache.shardingsphere.sharding.execute.sql.execute.SQLExecuteCallback.execute(SQLExecuteCallback.java:58)
at org.apache.shardingsphere.underlying.executor.engine.ExecutorEngine.lambda$asyncExecute$0(ExecutorEngine.java:110)
at com.google.common.util.concurrent.TrustedListenableFutureTask$TrustedFutureInterruptibleTask.runInterruptibly(TrustedListenableFutureTask.java:111)
at com.google.common.util.concurrent.InterruptibleTask.run(InterruptibleTask.java:58)
at com.google.common.util.concurrent.TrustedListenableFutureTask.run(TrustedListenableFutureTask.java:75)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
原因
之所以会出现这个异常,是因为MySQL5.0以后针对超长时间DB连接做了一个处理,那就是如果一个DB连接在无任何操作情况下过了8个小时后(Mysql 服务器默认的“wait_timeout”是8小时),Mysql会自动把这个连接关闭。这就是问题的所在,在连接池中的connections如果空闲超过8小时,mysql将其断开,而连接池自己并不知道该connection已经失效,如果这时有 Client请求connection,连接池将该失效的Connection提供给Client,将会造成上面的异常。
所以配置datasource时需要配置相应的连接池参数,定是去检查连接的有效性,定时清理无效的连接。
解决方案
尝试1
设置数据源中配置
druid.test-on-borrow=true
druid.test-on-return=false
druid.test-while-idle=true
druid.time-between-eviction-runs-millis=60000
druid.min-evictable-idle-time-millis=3600000
druid.validation-query=SELECT 1 FROM DUAL
尝试2
在jvm启动参数中增加如下配置:
-Ddruid.mysql.usePingMethod=false
尝试3
数据库url配置中增加这个配置:&autoReconnect=true
问题依然存在
基本上4分钟报一次,不过不影响正常业务逻辑
先暂时搁置
原文链接:
https://blog.csdn.net/qq_38023253/article/details/80815618
https://blog.csdn.net/znb769525443/article/details/109752364
https://www.renfei.net/posts/1003414
https://www.zzm8.com/article/193772.html
这篇关于discard connection的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!