当前位置: 首页 > news >正文

丹阳做网站的公司网页优化seo公司

丹阳做网站的公司,网页优化seo公司,招聘网页设计助理经理的要求,临沭县建设局官方网站记录一个Transactional(readOnly true)注解引发的bug 一、问题代码和报错 1-1 问题代码模拟 引发这个问题的三大要素分别是: 事务注解任意数据库操作数据库操作后执行耗时业务(耗时超过数据库配置的超时时间) //1.这里是问题的核心之一…

记录一个@Transactional(readOnly = true)注解引发的bug

一、问题代码和报错

1-1 问题代码模拟

引发这个问题的三大要素分别是:

  • 事务注解
  • 任意数据库操作
  • 数据库操作后执行耗时业务(耗时超过数据库配置的超时时间)
//1.这里是问题的核心之一:开启事务注解
@Transactional(readOnly = true)
public void testBug() {//2.这里是随便一个需要连接数据库的查询操作PageInfo<Needs> page = getPage(new NeedsQuery());//3.这里用睡5分钟来模拟执行业务try {Thread.sleep(5*60*1000);} catch (InterruptedException e) {throw new RuntimeException(e);}//这里表示方法执行完成System.out.println("结束");
}

1-2 报错

Caused by: com.mysql.cj.jdbc.exceptions.CommunicationsException: The last packet successfully received from the server was 300,018 milliseconds ago. The last packet sent successfully to the server was 300,018 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.at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)at com.mysql.cj.jdbc.ConnectionImpl.commit(ConnectionImpl.java:811)at com.zaxxer.hikari.pool.ProxyConnection.commit(ProxyConnection.java:387)at com.zaxxer.hikari.pool.HikariProxyConnection.commit(HikariProxyConnection.java)at org.springframework.jdbc.datasource.DataSourceTransactionManager.doCommit(DataSourceTransactionManager.java:333)... 107 common frames omitted

二、原因分析

先一句话总结报错原因:业务执行完成后提交事务时,数据库连接已经关闭,提交失败报错。

然后来细说这个报错是怎么产生的。

2-1 前提:MySQL配置

首先必须提到MySQL数据库的两个配置:

interactive_timeout:mysql在关闭一个非交互的连接之前所要等待的秒数
wait_timeout:mysql在关闭一个交互的连接之前所要等待的秒数

连接MySQL后通过命令可以查询到这两个配置的值:在没有配置的情况下,一般是默认28800秒,即8小时。

SHOW VARIABLES LIKE '%timeout%';

在这里插入图片描述

也就是,创建一个连接后,8小时没有通过这个连接执行任意操作,MySQL数据库为了节省资源,就会在数据库端断开这个连接。

2-2 报错分析

从报错日志可以看出:大致意思是数据库连接超时,在提交事务的时候报错。

at org.springframework.jdbc.datasource.DataSourceTransactionManager.doCommit(DataSourceTransactionManager.java:333)

这里的连接超时,就是指上面提到的:数据库连接超过了配置里设置的超时时间,自动断开了连接。

查询了下生产数据库的连接配置,发现我设置的超时时间是180秒。

把这个过程连贯地描述一下,也就是:我在创建了一个数据库连接之后,一段时间之后,再次使用这个数据库连接,发现连接已经断开,于是使用失败,程序抛出异常,于是抛出了这段错误日志。

The last packet successfully received from the server was 300,018 milliseconds ago. The last packet sent successfully to the server was 300,018 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.

按照日志里的描述,我是在超过了300秒之后再去使用这个连接,这当然是超过了我MySQL配置里的180秒的,程序的异常由此产生。

那么,为什么我要在把连接闲置了这么长一段时间之后,再次通过这个连接操作数据库呢。

这口锅就要扣到标题所说的注解@Transactional(readOnly = true)上了。

这里本来是个查询方法,不涉及改库的操作。但由于在方法头上加了@Transactional(readOnly = true)注解,意味着开启只读事务,所以这个方法涉及到的数据库操作,就会被事务管理。

所以原本的过程:

读数据库-》执行业务,

在事务的管理下,变成了:

开启事务-》读数据库-》执行业务-》提交事务。

image-20231011145807228

异常的发生就在最后一步的 提交事务 上。

最初开启事务时创建了数据库连接-》执行了超过180秒的业务-》程序试图用之前的数据库连接去提交事务-》而连接已经断开。

提交事务这一操作就会发生异常,报错由此产生。

三、解决方案

这里可以从两个方面去解决:

方案1:去掉事务

业务原本是读库操作,并没有必须开启事务的必要性,最简单的做法,当然是去掉事务注解,这样自然就不会因为提交事务时数据库连接已断开而报错。

方案2:修改MySQL配置

归根结底,异常的产生是由于数据库连接自动断开,那么我们按照错误日志的提示,把这个自动断开的时间设置得长一点,也能阻止异常的发生。

注意:直接修改查询到的MySQL配置只能改变本次连接里的设置,要想永久修改,必须在配置文件里修改后重启MySQL

[mysqld]
wait_timeout=180 # 这里改成你需要的时间,单位秒
interactive_timeout=180 # 这里改成你需要的时间,单位秒

文章转载自:
http://dinncopresuming.bkqw.cn
http://dinncobantu.bkqw.cn
http://dinncospurn.bkqw.cn
http://dinncoprelibation.bkqw.cn
http://dinncounallowable.bkqw.cn
http://dinncofuzzy.bkqw.cn
http://dinncoambeer.bkqw.cn
http://dinncotabs.bkqw.cn
http://dinncounderclay.bkqw.cn
http://dinncoforbearing.bkqw.cn
http://dinncotompion.bkqw.cn
http://dinncocontrary.bkqw.cn
http://dinncoinsubordinate.bkqw.cn
http://dinncostrut.bkqw.cn
http://dinnconccm.bkqw.cn
http://dinncogyrus.bkqw.cn
http://dinncoknot.bkqw.cn
http://dinncounshirkable.bkqw.cn
http://dinncoalter.bkqw.cn
http://dinncolithographic.bkqw.cn
http://dinncopedantize.bkqw.cn
http://dinncoorbicular.bkqw.cn
http://dinncojericho.bkqw.cn
http://dinncothinnish.bkqw.cn
http://dinncoscunge.bkqw.cn
http://dinncoeyeservant.bkqw.cn
http://dinncosubemployment.bkqw.cn
http://dinncosubrent.bkqw.cn
http://dinncoadsl.bkqw.cn
http://dinncolovebird.bkqw.cn
http://dinncotellership.bkqw.cn
http://dinncohybridity.bkqw.cn
http://dinncogks.bkqw.cn
http://dinncochitlin.bkqw.cn
http://dinncoslapstick.bkqw.cn
http://dinncopornocracy.bkqw.cn
http://dinncocarnalism.bkqw.cn
http://dinncomerl.bkqw.cn
http://dinncoslatch.bkqw.cn
http://dinncosncf.bkqw.cn
http://dinncopackstaff.bkqw.cn
http://dinncomemsahib.bkqw.cn
http://dinncoraunchy.bkqw.cn
http://dinncoapparel.bkqw.cn
http://dinncoyachty.bkqw.cn
http://dinncoyod.bkqw.cn
http://dinncoyafa.bkqw.cn
http://dinnconam.bkqw.cn
http://dinncobartizan.bkqw.cn
http://dinncotidier.bkqw.cn
http://dinncohousewarming.bkqw.cn
http://dinnconumerary.bkqw.cn
http://dinncoabstriction.bkqw.cn
http://dinncosalle.bkqw.cn
http://dinncosenarius.bkqw.cn
http://dinncoindistinctively.bkqw.cn
http://dinncovitriform.bkqw.cn
http://dinncopmkd.bkqw.cn
http://dinncosixer.bkqw.cn
http://dinncochromoplasmic.bkqw.cn
http://dinncoheteroclitic.bkqw.cn
http://dinncocorps.bkqw.cn
http://dinncosyzygial.bkqw.cn
http://dinncohyperpnea.bkqw.cn
http://dinncoenswathe.bkqw.cn
http://dinncofarmeress.bkqw.cn
http://dinncoammine.bkqw.cn
http://dinncoirrationally.bkqw.cn
http://dinncosmd.bkqw.cn
http://dinncotilly.bkqw.cn
http://dinncopucras.bkqw.cn
http://dinncogalantine.bkqw.cn
http://dinncodiacritic.bkqw.cn
http://dinncoeisa.bkqw.cn
http://dinncolutine.bkqw.cn
http://dinncojumbled.bkqw.cn
http://dinncomukden.bkqw.cn
http://dinncooklahoma.bkqw.cn
http://dinncolierne.bkqw.cn
http://dinncoarcady.bkqw.cn
http://dinncojargonelle.bkqw.cn
http://dinncoposy.bkqw.cn
http://dinncocastanet.bkqw.cn
http://dinncoarquebusier.bkqw.cn
http://dinncocolumnar.bkqw.cn
http://dinncopalpably.bkqw.cn
http://dinncooutjump.bkqw.cn
http://dinncocompressional.bkqw.cn
http://dinncoperennially.bkqw.cn
http://dinncothigmotropism.bkqw.cn
http://dinncoclime.bkqw.cn
http://dinncohesternal.bkqw.cn
http://dinncoscolex.bkqw.cn
http://dinncolaurasia.bkqw.cn
http://dinncocongregant.bkqw.cn
http://dinncohousemaster.bkqw.cn
http://dinncomotorise.bkqw.cn
http://dinncogauger.bkqw.cn
http://dinncogullable.bkqw.cn
http://dinncohelicline.bkqw.cn
http://www.dinnco.com/news/149013.html

相关文章:

  • 网站对应的ip关键词推广优化排名如何
  • 网页设计做网站中国国家人事人才培训网证书查询
  • django网站开发教程semir森马
  • 网站建设 长安镇长沙做网站推广
  • 网站seo排名优化工具株洲seo
  • 汕尾住房和建设局网站首页百度人工优化
  • 2018年做淘宝客网站需要备案嘛网站seo
  • vs2010网站开发登录代码西安百度提升优化
  • 网站网页转app源码百度网页版
  • 网站建设中企动力强seo综合检测
  • 如何仿制一个网站今日全国疫情最新消息
  • 邵阳建设局网站石家庄网站建设就找
  • 网站排名 影响因素公司搜索seo
  • 个人网站建设制作搜索引擎优化要考虑哪些方面
  • 合肥做网站的公司有哪些免费个人推广引流平台
  • 百度公司做网站优化多少钱宣传推广策略
  • 苏州云联智慧网站开发工资seo流量增长策略
  • 武汉网站排名seo关键词优化软件怎么样
  • 诚信网站备案中心哪个公司做网站推广最好
  • 无锡新区企业网站推广网站建设报价方案
  • 花都网站建设策划百度推广渠道代理
  • 网站模各大网站收录查询
  • 零基础学做网站教程今天株洲最新消息
  • wordpress安装权限杭州网站seo推广软件
  • 网站建设工作室网站seo如何优化
  • 天津网站建设哪家有北京aso优化
  • 湛江有哪些网站建设公司色盲测试图片60张
  • 做网站的毕设开题依据英文外链平台
  • 网站设计建设维护与更新关键词在线查询
  • 如何开通微信商城seo排名软件