mysql No operations allowed after connection closed连接异常的解决

Spring Boot多数据源配置及No operations allowed after connection closed连接异常的解决

最近项目上线,遇到了一个诡异的bug。

首先说下我的项目配置: SpringBooot + SpringMVC+SpringData JPA+ 两个MySql

也就是我这个项目配置了多数据源。

前期开发是没什么问题的,一切运转良好。

但是等到项目上线测试时,经常第二天测试人员就过来报告项目挂掉了。

经过查看日志才发现下面这个异常:

2018-01-27 08:09:15,361 - Servlet.service() for servlet [dispatcherServlet] in context with path [/shop] threw exception [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: 断开的管道 (Write failed)......

注意排查异常要抓住重点: No operations allowed after connection closed。

从这个地方我们知道是mysql的链接关闭了已经。访问一个关闭了的链接当然会出现异常了。

原因:

之所以会出现这个异常,是因为MySQL5.0以后针对超长时间DB连接做了一个处理,那就是如果一个DB连接在无任何操作情况下过了8个小时后(Mysql 服务器默认的“wait_timeout”是8小时),Mysql会自动把这个连接关闭。这就是问题的所在,在连接池中的connections如果空闲超过8小时,mysql将其断开,而连接池自己并不知道该connection已经失效,如果这时有 Client请求connection,连接池将该失效的Connection提供给Client,将会造成上面的异常。
所以配置datasource时需要配置相应的连接池参数,定是去检查连接的有效性,定时清理无效的连接。

那解决方法是什么呢?

在application.properties的两个数据源的配置下添加如下连接池配置:

#以下为连接池的相关参数配置
spring.datasource.primary.max-idle=10
spring.datasource.primary.max-wait=10000
spring.datasource.primary.min-idle=5
spring.datasource.primary.initial-size=5
spring.datasource.primary.validation-query=SELECT 1
spring.datasource.primary.test-on-borrow=false
spring.datasource.primary.test-while-idle=true
spring.datasource.primary.time-between-eviction-runs-millis=18800
#以下为连接池的相关参数配置
spring.datasource.secondary.max-idle=10
spring.datasource.secondary.max-wait=10000
spring.datasource.secondary.min-idle=5
spring.datasource.secondary.initial-size=5
spring.datasource.secondary.validation-query=SELECT 1
spring.datasource.secondary.test-on-borrow=false
spring.datasource.secondary.test-while-idle=true
spring.datasource.secondary.time-between-eviction-runs-millis=18800

之前也说了因为我是两个数据源,所以配置了两次。当然如果你只有一个数据源,配置一个就可以了。

这样上述异常就得到解决,其实出现该问题的原因,还是自己本人当时忽略了连接池的相关配置,以及对一些常见连接池的配置尚有欠缺,这里再补充一个小知识点,spring boot默认会优先使用的连接池是tomcat连接池,前提是在tomcat连接池可用的情况下

  • 11
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 17
    评论
"No operations allowed after connection closed"是一个MySQL数据库错误,它表示在连接关闭之后不允许进行任何操作。这个错误通常发生在连接池中的连接被关闭后,但连接池并不知道该连接已失效,然后将该失效的连接提供给客户端,导致异常的情况下。 解决这个问题的方法是,可以通过在连接池中设置合适的超时时间来避免连接过期。MySQL 5.0之后,针对超长时间的数据库连接做了处理,超过8小时没有任何操作的连接会被自动关闭,默认超时时间是8小时。因此,可以将连接池中的连接空闲时间设置为小于8小时,以确保连接不会被自动关闭。 另外,还可以在代码中捕获这个异常并处理,例如在捕获到这个异常后重新建立一个新的连接,以确保操作顺利进行。 综上所述,解决"No operations allowed after connection closed"的方法是:1. 在连接池中设置合适的超时时间,2. 在代码中捕获并处理这个异常。这样可以避免连接过期导致的异常情况的发生。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [No operations allowed after connection closed连接异常解决](https://blog.csdn.net/weixin_35852328/article/details/81977000)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [No operations allowed after connection closed](https://blog.csdn.net/y534560449/article/details/116240458)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值