博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
解决java.net.SocketException: Connection timed out (Write failed)
阅读量:6452 次
发布时间:2019-06-23

本文共 9401 字,大约阅读时间需要 31 分钟。

hot3.png

异常堆栈信息 

[Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is javax.persistence.PersistenceException: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.] with root cause java.net.SocketException: Connection timed out (Write failed)	at java.net.SocketOutputStream.socketWrite0(Native Method)	at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:111)	at java.net.SocketOutputStream.write(SocketOutputStream.java:155)	at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)	at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)	at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3731)	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2512)	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2683)	at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2482)	at com.mysql.jdbc.ConnectionImpl.setAutoCommit(ConnectionImpl.java:4796)	at sun.reflect.GeneratedMethodAccessor127.invoke(Unknown Source)	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)	at java.lang.reflect.Method.invoke(Method.java:498)	at org.apache.tomcat.jdbc.pool.ProxyConnection.invoke(ProxyConnection.java:126)	at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:108)	at org.apache.tomcat.jdbc.pool.interceptor.AbstractCreateStatementInterceptor.invoke(AbstractCreateStatementInterceptor.java:79)	at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:108)	at org.apache.tomcat.jdbc.pool.DisposableConnectionFacade.invoke(DisposableConnectionFacade.java:81)	at com.sun.proxy.$Proxy83.setAutoCommit(Unknown Source)	at org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.begin(AbstractLogicalConnectionImplementor.java:67)	at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.begin(LogicalConnectionManagedImpl.java:238)	at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.begin(JdbcResourceLocalTransactionCoordinatorImpl.java:214)	at org.hibernate.engine.transaction.internal.TransactionImpl.begin(TransactionImpl.java:52)	at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1512)	at org.hibernate.jpa.internal.TransactionImpl.begin(TransactionImpl.java:45)	at org.springframework.orm.jpa.vendor.HibernateJpaDialect.beginTransaction(HibernateJpaDialect.java:189)	at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:380)	at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:377)	at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:461)	at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:277)	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:671)	at com.annet.hospital.applet.service.user.PatientServiceImpl$$EnhancerBySpringCGLIB$$825266d.getRecentConsultDoctor(
) at com.annet.hospital.applet.controller.user.PatientController.recentDoctors(PatientController.java:132) at sun.reflect.GeneratedMethodAccessor357.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) at javax.servlet.http.HttpServlet.service(HttpServlet.java:661) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1468) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748)

项目使用Springboot 1.5.13.RELEASE,默认的数据库连接池是tomcat-jdbc。

出现这种异常的原因是:

Mysql 服务器默认的“wait_timeout”是8小时,也就是说一个connection空闲超过8个小时,Mysql将自动断开该connection。 这就是问题的所在,在连接池中的connections如果空闲超过8小时,mysql将其断开,而连接池并不知道该connection已经失效,如果这时有 Client请求connection,连接池将该失效的Connection提供给Client,将会造成上面的异常。

至于出现:No operations allowed after connection closed。这样的问题原因只有一个,你这里和数据库的连接Connection是一个Static的,程序共享这一个Connection。所以第一 次对数据库操作没问题,当把Connection关闭后,第二次还想操作数据库时Connection肯定不存在了。

我的个人配置如下

#MYSQL 8小时连接超时配置#验证连接的有效性spring.datasource.test-while-idle=true#获取连接时候验证,会影响性能spring.datasource.test-on-borrow=truespring.datasource.validation-query=SELECT 1 #空闲连接回收的时间间隔,与test-while-idle一起使用,设置5分钟spring.datasource.time-between-eviction-runs-millis=300000#连接池空闲连接的有效时间 ,设置30分钟spring.datasource.min-evictable-idle-time-millis=1800000

 

转载于:https://my.oschina.net/pipimao/blog/2253885

你可能感兴趣的文章
Important Log Locations for Grid
查看>>
ThinkPad T400所有驱动下载
查看>>
Python爬虫从入门到放弃(二十二)之 爬虫与反爬虫大战
查看>>
kvm虚拟化学习笔记(二十)之convirt安装linux系统
查看>>
python中关于中文报错的解决办法
查看>>
XSS研究1-来自内部的XSS攻击
查看>>
shell实战:内置(built-in)变量
查看>>
查看自己的ip和采用什么方式上网(网通/电信)
查看>>
如何在程序打开时窗口恢复上次关闭的大小和位置?
查看>>
关于Java String对象创建问题解惑
查看>>
应该是Angular2的一个bug?
查看>>
Eclipse(Helios) 初始化失败的问题
查看>>
SQL Server 2012 数据库镜像配置完整篇
查看>>
MariaDB 10之TokuDB存储引擎
查看>>
Flex与.NET互操作(一):基于Socket的网络连接
查看>>
WPF中Style的使用
查看>>
组策略设置服务器安全-----不显示最后的用户名
查看>>
慢查询日志的另外一种方法
查看>>
引发CPU占用率快100%的可能原因
查看>>
公司今年的一道校招笔试题--五猴分桃
查看>>