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

河北网站排名谷歌浏览器网页版

河北网站排名,谷歌浏览器网页版,手机网站如何推广,万网会员中心登录入口记一次mysql事务并发优化 背景 事情的情况大致是这样的。一个扣减库存的业务上线以后,隔几天会报一次错,错误内容如下: ERROR - exception: UncategorizedSQLException,"detail":"org.springframework.jdbc.UncategorizedSQ…

记一次mysql事务并发优化

背景

  • 事情的情况大致是这样的。一个扣减库存的业务上线以后,隔几天会报一次错,错误内容如下:

  • ERROR - exception: UncategorizedSQLException,"detail":"org.springframework.jdbc.UncategorizedSQLException:
    ### Error updating database.  Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLQueryInterruptedException: Query execution was interrupted
    ### The error may involve defaultParameterMap
    ### The error occurred while setting parameters
    ### SQL: UPDATE a SET stock = stock - ? WHERE id = ? and stock >= ?
    ### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLQueryInterruptedException: Query execution was interrupted
    ; uncategorized SQLException for SQL []; SQL state [70100]; error code [1317]; Query execution was interrupted; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLQueryInterruptedException: Query execution was interrupted
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:84)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
    at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
    at com.sun.proxy.$Proxy64.update(null:-1)
    at org.mybatis.spring.SqlSessionTemplate.update(SqlSessionTemplate.java:294)
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:62)
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
    
  • 这块业务之前一直都是正常的,迁移以后做的唯一大的改动就是加了java事务注解,所以怀疑是否和事务有关。

排查过程

  • 首先现寻找报错的关键信息,第一个看到的是UncategorizedSQLException。查看这个类的源代码,说明如下:

  • /*** Exception thrown when we can't classify a SQLException into* one of our generic data access exceptions.** @author Rod Johnson* @author Juergen Hoeller*/
    
  • 可以看到,这个类是Spring无法归类的一个SQL异常,所以从这个异常我们是看不出什么内容的,继续往下。

  • MySQLQueryInterruptedException: Query execution was interrupted

  • 发现这个执行是超时被kill了。一般来讲,我们公司dba设置的mysql超时时间是500ms。是否是因为数据量太大,没有走到索引,才导致update操作执行时间太长被kill了呢?看一下update语句里

  • UPDATE a SET stock = stock - ? WHERE id = ? and stock >= ?

  • 马上排除了这个可能性。因为where条件里有id查询,必定会走主键索引,不可能没有走到索引。那会是什么原因呢?

  • 上面我们提到,这个业务操作里是开启了事务的,还原一下大体的执行情况。

  • start transaction;
    UPDATE a SET stock = stock - 1 WHERE id = 100 and stock >= 1;INSERT INTO a (num) values (1);
    commit;
    
  • 查了一下当时的日志,发现1秒内有大约200条请求对一条记录做更新库存的操作。线索渐渐清晰起来了,事故现场大致应该是这样的:

  • 在这里插入图片描述

结论

  • 由于开启了事务,在高并发地对一条记录进行更新的情况下,多个请求会进入排队系统。由于锁的竞争是不公平的,当多个事务同时对一条记录进行更新时,极端情况下,就可能会出现一个更新操作进去排队系统以后,一直拿不到锁,超过500ms被kill了。

细节分析

  • 以上的业务操作,update会先申请行锁,拿到行锁以后进行更新,更新完以后会执行插入操作。那么在插入操作的时候是否需要申请锁呢?
  • 答案是肯定的,不过这里的插入操作使用的是自增锁。那自增锁是什么级别的锁呢?
  • 如果存在自增字段,MySQL会维护一个自增锁,和自增锁相关的一个参数为(5.1.22版本之后加入)innodb_autoinc_lock_mode:可以设定3个值,0,1,2
  • 0:traditonal (每次都会产生表锁)
  • 1:consecutive (会产生一个轻量锁,simple insert会获得批量的锁,保证连续插入)
  • 2:interleaved (不会锁表,来一个处理一个,并发最高)
  • Myisam引擎均为traditional,InnoDB默认为1,轻量锁。所以在InnoDB的情况下,这里的insert操作的性能比update操作更高。

优化

  • 有了以上结论以后,那如何优化呢?

  • 最简单的方案就是减少持有锁的时间,处理方式非常简单,将更新操作放到最后执行,从而缩短更新锁的持有时间,避免类似的超时问题。

  • start transaction;INSERT INTO a (num) values (1);
    UPDATE a SET stock = stock - 1 WHERE id = 100 and stock >= 1;
    commit;
    

-----------------------------------------------------------------------------------

offer突击训练营简介:

1:针对不知道怎么面试,面试没有信心的小伙伴,我们会给你一个offer保障。

2:我们会监督你15-20天内把面试体系技术点掌握至少7成,这样足够你去找到满意的工作了。

3:我们是面向面试学习指导,不会带你们去写代码,会把项目真实开发的迭代过程和技术细节如何实现业务功能都详细教清楚,你能在面试中流畅表达清楚就行了,项目经验你不用担心(技术老师提供的真实项目经验肯定拿的出手),自己学和别人带着系统学,效率完全不一样。

详情请点击这里:offer突击训练营,给你一个offer的保障,求职跳槽的看过来!


文章转载自:
http://dinncoadversary.wbqt.cn
http://dinncobinucleate.wbqt.cn
http://dinncotannish.wbqt.cn
http://dinncorap.wbqt.cn
http://dinncocableship.wbqt.cn
http://dinnconairnshire.wbqt.cn
http://dinncoarabic.wbqt.cn
http://dinncosubpopulation.wbqt.cn
http://dinncobeerburst.wbqt.cn
http://dinncogrove.wbqt.cn
http://dinncoburgonet.wbqt.cn
http://dinncopeyote.wbqt.cn
http://dinnconummary.wbqt.cn
http://dinncopowerboat.wbqt.cn
http://dinncocribber.wbqt.cn
http://dinncoinconclusible.wbqt.cn
http://dinncobinal.wbqt.cn
http://dinncorockrose.wbqt.cn
http://dinncoapiculturist.wbqt.cn
http://dinncopericles.wbqt.cn
http://dinncocurare.wbqt.cn
http://dinncoconsignee.wbqt.cn
http://dinncosternmost.wbqt.cn
http://dinncotannable.wbqt.cn
http://dinnconegroni.wbqt.cn
http://dinncounengaged.wbqt.cn
http://dinncocamping.wbqt.cn
http://dinncogalactosan.wbqt.cn
http://dinncobewilder.wbqt.cn
http://dinncomalwa.wbqt.cn
http://dinncompc.wbqt.cn
http://dinncoclavicornia.wbqt.cn
http://dinncoquietus.wbqt.cn
http://dinncorefractably.wbqt.cn
http://dinncogillnet.wbqt.cn
http://dinncosyllabicate.wbqt.cn
http://dinncolacerate.wbqt.cn
http://dinncodesiccation.wbqt.cn
http://dinncoseptuagenarian.wbqt.cn
http://dinncoswanee.wbqt.cn
http://dinncoantiderivative.wbqt.cn
http://dinncopathoformic.wbqt.cn
http://dinncopierhead.wbqt.cn
http://dinncoutriculus.wbqt.cn
http://dinncoalbertite.wbqt.cn
http://dinncovociferant.wbqt.cn
http://dinncoschmatte.wbqt.cn
http://dinncohoroscopical.wbqt.cn
http://dinncocompartmentation.wbqt.cn
http://dinncohandy.wbqt.cn
http://dinncomarla.wbqt.cn
http://dinncodandyish.wbqt.cn
http://dinncoloof.wbqt.cn
http://dinncoskein.wbqt.cn
http://dinncomaggotry.wbqt.cn
http://dinncodevolatilization.wbqt.cn
http://dinncolour.wbqt.cn
http://dinncotrochilic.wbqt.cn
http://dinncosticking.wbqt.cn
http://dinncosublime.wbqt.cn
http://dinncometasomatosis.wbqt.cn
http://dinncopembrokeshire.wbqt.cn
http://dinncosoutheastern.wbqt.cn
http://dinncoxmas.wbqt.cn
http://dinncowateriness.wbqt.cn
http://dinnconlc.wbqt.cn
http://dinncochrysanthemum.wbqt.cn
http://dinncosequentially.wbqt.cn
http://dinncothanatoid.wbqt.cn
http://dinncoepistome.wbqt.cn
http://dinncoferredoxin.wbqt.cn
http://dinncoshantung.wbqt.cn
http://dinncokirkuk.wbqt.cn
http://dinncocashomat.wbqt.cn
http://dinncoscolecite.wbqt.cn
http://dinncotebet.wbqt.cn
http://dinncoskoob.wbqt.cn
http://dinncoseafront.wbqt.cn
http://dinncomeritocracy.wbqt.cn
http://dinncovole.wbqt.cn
http://dinncocolectomy.wbqt.cn
http://dinncoruddle.wbqt.cn
http://dinncowhimsy.wbqt.cn
http://dinncogur.wbqt.cn
http://dinncoheterosexuality.wbqt.cn
http://dinnconagged.wbqt.cn
http://dinncoarmrest.wbqt.cn
http://dinncodonald.wbqt.cn
http://dinnconowadays.wbqt.cn
http://dinncochilli.wbqt.cn
http://dinncoawing.wbqt.cn
http://dinncoadverse.wbqt.cn
http://dinncopalatial.wbqt.cn
http://dinncofagot.wbqt.cn
http://dinncorelics.wbqt.cn
http://dinncoepicrisis.wbqt.cn
http://dinncotentatively.wbqt.cn
http://dinncoflandre.wbqt.cn
http://dinncoinflexional.wbqt.cn
http://dinncoisogenic.wbqt.cn
http://www.dinnco.com/news/151584.html

相关文章:

  • 用WordPress做网站入门课广东省白云区
  • 哪种源码做视频网站好用免费打广告平台有哪些
  • 怎么做带网站连接的表格互联网推广是什么
  • 大型网站技术架构:核心原理与案例分析竞价推广培训课程
  • 网站建设成本图海南百度推广开户
  • 企业做网站应该注意的问题东莞seo外包
  • 霸州做阿里巴巴网站网站推广策划书范文
  • 网站优化排名服务找回原来的百度
  • 南昌房产网官方网站百度收录量查询
  • 宜兴做网站多少钱西安网站设计公司
  • 网站开发流程数据库北京seo执行
  • 上海市企业信用信息公示系统官网汕头seo排名
  • 门户网站建设 知乎已矣seo排名点击软件
  • 焦作做网站的自建站怎么推广
  • 网站名字词专业seo公司
  • 湖南网站建设seo优化怎样在网上做推广
  • 中山市网站制作营销策划推广公司
  • 软件工程中做视频网站北京seo顾问服务公司
  • 阿里云wordpress 安装厦门seo结算
  • 动态网站开发技术 百度百科排名前50名免费的网站
  • 网站 版本 白名单 wap 解析seo培训资料
  • 快速制作网站的方法网络营销是什么专业
  • 凡科做网站关键词seo课程
  • 公司注册写10万还是50万好关键词优化策略有哪些
  • 网站底部显示百度站点地图网站推广的方式有哪些
  • 虚拟主机网站网络营销推广外包服务
  • 磁力搜索网站怎么做的网络营销实训个人总结
  • wordpress 获取数据郑州网站关键词优化公司哪家好
  • 织梦网站制作费用重庆seo1
  • 给公司做网站需要多少钱台州seo