记录一次cpu被干满的场景


某个服务的cpu突然被干到100%的场景

模拟sql

<select id="batchUpdateStatus">
    update xxx_account set status = #{status}
    <where>
      <if test="idList != null and idList.size > 0">
        and id in
        <foreach item="item" collection="idList" separator="," close=")" open="(" index="index">
          #{item}
        </foreach>
      </if>
    </where>
  </select>

在B端触发一个场景,导致xxx服务中的某个接口在执行sql时,发现cpu被干到100%。

这个接口是对一个数据的update 逻辑,根据sql,为啥这个堆栈里面出现了select 操作。

排查发现

是因为阿里云的mysql的msha驱动中,update语句的xml的标签写成select了,非官方驱动会触发updateCount不更新的问题,导致一直查result。

解决方案

修改sql的xml的标签。

    <update id="batchUpdateStatus">
        update account set status = #{status}
        <where>
            <if test="idList != null and idList.size > 0">
                and id in
                <foreach item="item" collection="idList" separator="," close=")" open="(" index="index">
                    #{item}
                </foreach>
            </if>
        </where>
    </update>

后续复盘:对于代码中的sql标签的使用,要规范!

常见错误
SpringBoot
Mysql
Mybatis