首页 > 数据库 >spring boot jpa MYSQL教程mysql连接的空闲时间超过8小时后 MySQL自动断开该连接

spring boot jpa MYSQL教程mysql连接的空闲时间超过8小时后 MySQL自动断开该连接

时间:2023-04-27 17:58:54浏览次数:44  
标签:jpa spring idle datasource timeout MySQL 连接 wait

 

Sun Apr 16 08:15:36 CST 2023
There was an unexpected error (type=Internal Server Error, status=500).
PreparedStatementCallback; SQL [select userId from familyxiao_UserConnection where providerId = ? and providerUserId = ?]; No operations allowed after connection closed.; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.

 

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."

 

解决这个问题的办法有三种:

1. 增加 MySQL 的 wait_timeout 属性的值.

修改 /etc/mysql/my.cnf文件,在 [mysqld] 节中设置:

# Set a connection to wait 8hours in idle status.
wait_timeout =86400
相关参数,红色部分
mysql> show variables like '%timeout%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| connect_timeout | 5 |
| delayed_insert_timeout | 300 |
| innodb_lock_wait_timeout | 50 |
| interactive_timeout | 28800 |
| net_read_timeout | 30 |
| net_write_timeout | 60 |
| slave_net_timeout | 3600 |
| wait_timeout | 28800 |
+--------------------------+-------+
同一时间,这两个参数只有一个起作用.到底是哪个参数起作用,和用户连接时指定的连接参数相关,缺省情况下是使用wait_timeout.我建议是将这两个参数都修改,以免引起不必要的麻烦.

这两个参数的默认值是8小时(60*60*8=28800).我测试过将这两个参数改为0,结果出人意料,系统自动将这个值设置为.换句话说,不能将该值设置为永久.
将这2个参数设置为24小时(60*60*24=604800)即可.
set interactive_timeout=604800;
set wait_timeout=604800;

2. 减少连接池内连接的生存周期,使之小于上一项中所设置的 wait_timeout 的值.
修改 c3p0 的配置文件,设置:

# How long to keep unused connections around(in seconds)
# Note: MySQL times out idle connections after 8hours(28,800seconds)
# so ensure this value is below MySQL idle timeout
cpool.maxIdleTime=25200
在 Spring 的配置文件中:

代码如下:
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="maxIdleTime"value="${cpool.maxIdleTime}"/>
<!--other properties -->
</bean>



3. 定期使用连接池内的连接,使得它们不会因为闲置超时而被 MySQL 断开.
修改 c3p0 的配置文件,设置:

# Prevent MySQL raise exception after a long idle timecpool.preferredTestQuery='SELECT 1'cpool.idleConnectionTestPeriod=18000cpool.testConnectionOnCheckout=true
修改 Spring 的配置文件:

代码如下:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="preferredTestQuery" value="${cpool.preferredTestQuery}"/>
<property name="idleConnectionTestPeriod" value="${cpool.idleConnectionTestPeriod}"/>
<property name="testConnectionOnCheckout" value="${cpool.testConnectionOnCheckout}"/>
<!--other properties --></bean>

转载请注明本页网址:
http://www.vephp.com/jiaocheng/4980.html

 

spring boot jpa

spring.datasource.max-active=100
spring.datasource.min-idle=10
spring.datasource.min-evictable-idle-time-millis = 1800000
spring.datasource.test-on-borrow=true
spring.datasource.test-on-return=true
spring.datasource.test-while-idle=true
spring.datasource.validation-query=select 1

 

标签:jpa,spring,idle,datasource,timeout,MySQL,连接,wait
From: https://www.cnblogs.com/xiaoruilin/p/17359771.html

相关文章

  • Spring17_配置文件知识要点5
    <bean>标签id属性:在容器中Bean实例的唯一标识,不允许重复class属性:要实例化的Bean的全限定名scope属性:Bean的作用范围,常用是Singleton(默认)和prototype<property>标签:属性注入,set方法注入使用name属性:属性名称va......
  • SpringCloud微服务架构分析说明!
    SpringCloud是一个基于SpringBoot的微服务框架,它提供了一系列的工具和组件,用于构建分布式系统中各个微服务之间的通信和互联,实现服务发现、负载均衡、分布式配置等功能。下面我们来具体解析一下SpringCloud微服务架构。服务注册与发现在微服务架构中,服务的数量非常多,因此需要一个机......
  • Spring17_配置文件依赖注入4
    一、Bean的依赖注入入门1.创建UserService,UserService内部再调用UserDao的save()方法 2.将UserServiceImpl的创建权交给Spring3.从Spring容器中获得UserService进行操作执行UserController中的main方法,检查控制台输出:二、Bean的依赖......
  • springboot分页插件的问题
    1:maven依赖的问题此类原因是与pom.xml文件中引入的分页依赖有关,由于springboot本身集成pagerhelper的分页插件,只需要引入如下依赖即可<!--spring-bootmybatispagehelper--><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-st......
  • SpringBoot配置日志文件定期切割
    下面是我的配置:创建logback-spring.xml写入下面的配置<?xmlversion="1.0"encoding="UTF-8"?><configurationdebug="false"><!--定义日志文件的存储地址勿在LogBack的配置中使用相对路径--><propertyname="LOG_HOME"value=&quo......
  • 蓝牙的扫描、连接、读写
    步骤:在info.plist中加入蓝牙的权限NSBluetoothAlwaysUsageDescription:创建蓝牙管理者对象,创建后,首先会执行系统蓝牙是否打开的协议方法centralManagerDidUpdateState,如果系统蓝牙未打开,会有系统的弹框提示打开蓝牙,如下:打开系统蓝牙后,开始扫描设备,扫描到设备后会执行didDis......
  • 连接metamask,并签名
    在assets下新建metamask.jsimport{ethers}from"ethers";//版本号为"ethers":"^4.0.47",import{buildLoginNonce}from'@/api/register'//请求接口后台返回随机验证码/****@param{*}ethereum连接钱包类型metamask(ethereum)、nabox(Nab......
  • Spring RestTemplate为何必须搭配MultiValueMap?
    微服务之间的大多都是使用HTTP通信,这自然少不了使用HttpClient。在不适用Spring前,一般使用ApacheHttpClient和OkHttpClient等,而一旦引入Spring,就有了更好选择-RestTemplate。 想接受一个Form表单请求,读取表单定义的两个参数para1和para2,然后作为响应返回......
  • Spring AOP 支持两种模式的动态代理
    SpringAOP支持两种模式的动态代理,JDKProxy或者cglib,jdkproxy:publicclassMyDynamicProxy{publicstaticvoidmain(String[]args){HelloImplhello=newHelloImpl();MyInvocationHandlerhandler=newMyInvocationHandler(hello);......
  • springboot入门时,发现Java版本与Spring boot版本无法对应导致错误的问题解决
    <?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/......