前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >mysql使用druid时自动断开连接解决方案

mysql使用druid时自动断开连接解决方案

作者头像
阿超
发布2022-08-16 15:42:24
5.6K0
发布2022-08-16 15:42:24
举报
文章被收录于专栏:快乐阿超

今天接手了一个上了年龄的项目,用的是smm,数据库连接池用的druid,数据库版本较老,是5.0的mysql,我升级成mysql8.0后,一开始还没问题,然后大概每次无连接2分钟后数据库自动断开。。。

前端妹子立马跟我说接口报错了,我咯噔心里一慌,让前端妹子给我截一下报错信息(后台日志刷的太多,当时没注意到)。然后前端妹子说,哦又好了,我当时意识到不对劲,但也没有深究,直到这样的问题出了几次,每次一刷新又好了,我开始意识到问题的严重性。

一开始我复制了报错信息,也就是这一段

代码语言:javascript
复制
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure

The last packet successfully received from the server was 8848 milliseconds ago.  The last packet sent successfully to the server was 8848 milliseconds ago.

搜了半天,网上说加配置、也就是在连接池配置加上

代码语言:javascript
复制
<property name="testOnBorrow" value="true" />
<property name="testWhileIdle" value="true" />

也就是这里,加上这两行,然而无事发生。。。两分钟后依旧报错

代码语言:javascript
复制
<!-- 数据库连接池 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSourc
e" destroy-method="close">
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="maxActive" value="10" />
		<property name="minIdle" value="5" />
		<property name="testOnBorrow" value="true" />
		<property name="testWhileIdle" value="true" />
	</bean>

于是我想到了用mysql的select(1)语句去保持连接,它不是两分钟断开嘛~

那我每分钟执行一次这个语句,保持mysql和服务端连接不就好了吗?

果然,写了个定时任务

代码语言:javascript
复制
@Resource
RefreshMysqlConnectionMapper refreshMysqlConnectionMapper;

@Scheduled(cron = "*/60 * * * * ?")
public void refreshMysqlConnection() {
		System.out.println("-----------------Performed task scheduling and maintained database connection" ); // 每60秒执行一次
		refreshMysqlConnectionMapper.refreshMysqlConnection();
	}

配置定时任务

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:task="http://www.springframework.org/schema/task"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.2.xsd">

	<context:component-scan base-package="com.ruben.task" />
	<!-- 开启task注解驱动 -->
	<task:annotation-driven />
</beans>

RefreshMysqlConnectionMapper.xml里面的sql语句,就这一条

代码语言:javascript
复制
<select id="refreshMysqlConnection" resultType="int">
    select (1)
  </select>

之后再也没出现过断开连接的错误~

还有其他的方式,比如修改数据库配置文件,让无连接后断开时间改久一点。

但无奈公司的DBA不在,数据库不让我碰,只好出此下策hh。好在问题解决了!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-06-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档