Spring Boot 配置MySQL数据库重连的操作方法

使用jdbc连接MySQL,如果连接失效,可能会报类似的错误:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 84,371,623 milliseconds ago.

The last packet sent successfully to the server was 78,860,631 milliseconds ago. 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.

如错误提示,可以在连接的url上添加autoReconnect=true来解决。

需要注意的是:mysql是不推荐使用autoReconnect配置,因为如果没有合适处理SQLException的话,它会带来一些数据一致性的副作用,可以参考:https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html中的autoReconect部分。

spring boot 1.4+需要看使用的是什么数据库连接池库,支持的连接池包括:tomcat, hikari, dbcp(1.5+废弃), dbcp2。

tomcat

spring.datasource.tomcat.test-on-borrow=true
spring.datasource.tomcat.validation-query=SELECT 1

dbcp2

spring.datasource.dbcp2.test-on-borrow=true
spring.datasource.dbcp2.validation-query=SELECT 1

总结

以上所述是小编给大家介绍的Spring Boot 配置MySQL数据库重连的操作方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!

您可能感兴趣的文章:

  • SpringBoot用JdbcTemplates访问Mysql实例代码
  • SpringBoot连接MYSQL数据库并使用JPA进行操作
  • SpringBoot入门系列之JPA mysql
(0)

相关推荐

  • SpringBoot连接MYSQL数据库并使用JPA进行操作

    今天给大家介绍一下如何SpringBoot中连接Mysql数据库,并使用JPA进行数据库的相关操作. 步骤一:在pom.xml文件中添加MYSQl和JPA的相关Jar包依赖,具体添加位置在dependencies中,具体添加的内容如下所示. <!--数据库相关配置--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-

  • SpringBoot入门系列之JPA mysql

    一,准备工作,建立spring-boot-sample-mysql工程 1.http://start.spring.io/ A.Artifact中输入spring-boot-sample-MySQL      B.勾选Web下的web      C.勾选SQL下的JPA MYSQL 2.Eclips中导入工程spring-boot-sample-mysql A.解压快捷工程spring-boot-sample-mysql到某文件夹 B.eclips中file->import->Import E

  • SpringBoot用JdbcTemplates访问Mysql实例代码

    本文介绍springboot通过jdbc访问关系型MySQL,通过spring的JdbcTemplate去访问. 准备工作 jdk 1.8 maven 3.0 idea mysql 初始化mysql: -- create table `account` DROP TABLE `account` IF EXISTS CREATE TABLE `account` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL,

  • Spring Boot 配置MySQL数据库重连的操作方法

    使用jdbc连接MySQL,如果连接失效,可能会报类似的错误: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 84,371,623 milliseconds ago. The last packet sent successfully to the server was 78,860,631 millisecond

  • spring boot配置MySQL数据库连接、Hikari连接池和Mybatis的简单配置方法

    此方法为极简配置,支持MySQL数据库多库连接.支持Hikari连接池.支持MyBatis(包括Dao类和xml文件位置的配置). 1.pom.xml中引入依赖: <!-- Begin of DB related --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId>

  • Spring Boot 添加MySQL数据库及JPA实例

    最近在学习Spring Boot,继续前面的学习,这一次我们加入MySQL数据库和JPA. 配置: pom.xml文件 <!-- 添加Mysql和JPA--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dep

  • Spring Boot高级教程之Spring Boot连接MySql数据库

    Spring Boot可以大大简化持久化任务,几乎不需要写SQL语句,在之前章节"Spring Boot 构建框架"中我们新建了一个Spring Boot应用程序,本章在原有的工程中与数据库建立连接. Spring Boot有两种方法与数据库建立连接,一种是使用JdbcTemplate,另一种集成Mybatis,下面分别为大家介绍一下如何集成和使用这两种方式. 1. 使用JdbcTemplate <dependency> <groupId>mysql</g

  • 通过Spring Boot配置动态数据源访问多个数据库的实现代码

    之前写过一篇博客<Spring+Mybatis+Mysql搭建分布式数据库访问框架>描述如何通过Spring+Mybatis配置动态数据源访问多个数据库.但是之前的方案有一些限制(原博客中也描述了):只适用于数据库数量不多且固定的情况.针对数据库动态增加的情况无能为力. 下面讲的方案能支持数据库动态增删,数量不限. 数据库环境准备 下面一Mysql为例,先在本地建3个数据库用于测试.需要说明的是本方案不限数据库数量,支持不同的数据库部署在不同的服务器上.如图所示db_project_001.d

  • springboot配置mysql数据库spring.datasource.url报错的解决

    目录 springboot配置mysql数据库spring.datasource.url报错 springboot下datasource连接配置 基本设置 datasource JPA jooq h2 JTA springboot配置mysql数据库spring.datasource.url报错 spring.datasource.url=jdbc:mysql://abc:3306/abcd?useUnicode=true&characterEncoding=utf8 很常规地配置了这个mysq

  • docker连接spring boot和mysql容器方法介绍

    在之前使用docker部署运行了Spring Boot的小例子,但是没有使用数据库.在这一篇中,介绍docker如何启动mysql容器,以及如何将Spring Boot容器与mysql容器连接起来运行. docker基本命令 首先熟悉一下在操作过程中常用的docker基本命令: docker images:列出所有docker镜像 docker ps:列出所有运行中的容器,-a参数可以列出所有容器,包括停止的 docker stop container_id:停止容器 docker start

  • Spring boot 使用mysql实例详解

    Spring boot 使用mysql实例详解 开发阶段用 H2即可,上线时,通过以下配置切换到mysql,spring boot将使用这个配置覆盖默认的H2. 1.建立数据库: mysql -u root CREATE DATABASE springbootdb 2.pom.xml: <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId&g

  • Spring Boot实战之数据库操作的示例代码

    上篇文章中已经通过一个简单的HelloWorld程序讲解了Spring boot的基本原理和使用.本文主要讲解如何通过spring boot来访问数据库,本文会演示三种方式来访问数据库,第一种是JdbcTemplate,第二种是JPA,第三种是Mybatis.之前已经提到过,本系列会以一个博客系统作为讲解的基础,所以本文会讲解文章的存储和访问(但不包括文章的详情),因为最终的实现是通过MyBatis来完成的,所以,对于JdbcTemplate和JPA只做简单演示,MyBatis部分会完整实现对文

  • spring boot创建和数据库关联模块详解

    创建步骤 1.导入依赖 2.配置文件 3.创建启动类 1.导入依赖 <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <scope>provided</scope> </dependency>

随机推荐