zoukankan      html  css  js  c++  java
  • 9. Lock wait timeout exceeded

    一、 现象

    用户打开消息推送有概率报错,后续发现推送消息阅读数、点赞数无法正常更新,mysql报警有行锁,

    DBA抓到有锁表语句,kill该语句未正常恢复,elk日志有大量的java.sql.SQLException: connection holder is null 重启应用恢复

    二、分析

    根据elk日志,出现cause by Lock wait timeout exceeded 的异常,经检查代码发现,有部分代码手动开启事务begin,但是走入分支造成结果却未提交事务。

    使得数据被锁长时间不能释放,(而系统当前也未做超时断开处理,导致异常情况发生时自愈能力不足)造成连锁反应,导致业务受到影响,最终引发系统崩溃。

    三、解决方法

    1.根据异常日志 connection holder is null,添加相关断开配置

    druid 
        remove-abandoned:true
        remove-abandoned-timeout:45000

    注意:如果存在长事务可能存在链接强行被回收的情况。

    2.spring框架管理事务

    (1)基于注解

    <!-- 添加事务管理器组件DataSourceTransactionManager -->
        <bean id="transactionManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <!-- 使用set方法注入数据源 -->
            <property name="dataSource" ref="dataSource"></property>
        </bean>
    
        <!-- 开启基于注解声明式事务 注意配置transaction-manager属性,它引用了我们事务管理组件对象,这里要和事务管理器组件id一致
            默认是transactionManager -->
        <tx:annotation-driven transaction-manager="transactionManager" />

    (2)基于xml

    <!-- 配置事务管理器 -->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <!-- 装配数据源 -->
            <constructor-arg name="dataSource" ref="dataSource"></constructor-arg>
        </bean>
        <!--使用xml配置事务方法 -->
        <aop:config>
            <!-- 设置添加事务的方法,使用切入点表达式-->
            <aop:pointcut expression="execution(* *.checkout(..))" id="mypoint"/>
            <!-- 将事务方法和事务的相关配置关联起来 -->
            <aop:advisor advice-ref="myAdvice" pointcut-ref="mypoint" />
        </aop:config>
        <!-- tx配置事务的属性 (使用tx名称空间)-->
        <tx:advice id="myAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <!-- 配置事务的属性,多个事务方法也可以在这个里面放,name设置事务方法名,propagation设置事务相关信息 -->
                <tx:method name="checkout" propagation="REQUIRED"/>
            </tx:attributes>
        </tx:advice>

    注意未被托管到的事务处理

  • 相关阅读:
    Linux IO接口 监控 (iostat)
    linux 防火墙 命令
    _CommandPtr 添加参数 0xC0000005: Access violation writing location 0xcccccccc 错误
    Visual Studio自动关闭
    Linux vsftpd 安装 配置
    linux 挂载外部存储设备 (mount)
    myeclipse 9.0 激活 for win7 redhat mac 亲测
    英文操作系统 Myeclipse Console 乱码问题
    Linux 基本操作命令
    linux 查看系统相关 命令
  • 原文地址:https://www.cnblogs.com/flgb/p/12942743.html
Copyright © 2011-2022 走看看