MySQL生产事故一例

勤奋不是嘴上说说而已,而是实际的行动,在勤奋的苦度中持之以恒,永不退却。业精于勤,荒于嬉;行成于思,毁于随。在人生的仕途上,我们毫不迟疑地选择勤奋,她是几乎于世界上一切成就的催产婆。只要我们拥着勤奋去思考,拥着勤奋的手去耕耘,用抱勤奋的心去对待工作,浪迹红尘而坚韧不拔,那么,我们的生命就会绽放火花,让人生的时光更加的闪亮而精彩。

导读:本篇文章讲解 MySQL生产事故一例,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

背景

线上日志报错:

18:57:54.985 [http-nio-8082-exec-9] ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: 
### Error updating database.  Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'inserttime' cannot be null
### The error may exist in class path resource [mapping/VarMonitorMapper.xml]
### The error may involve com.aaa.dao.VarMonitorMapper.insert-Inline
### The error occurred while setting parameters
### SQL: insert into var_monitor (var_id, inserttime, insertby, updatetime, updateby) values (?, ?, ?, ?, ?)
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'inserttime' cannot be null
; Column 'inserttime' cannot be null; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'inserttime' cannot be null] with root cause
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'inserttime' cannot be null

对应的建表语句:

inserttime timestamp default CURRENT_TIMESTAMP not null comment '插入时间',

分析

问题总结:
测试环境和生产环境 inserttime 字段的定义相同:inserttime timestamp default CURRENT_TIMESTAMP not null comment '插入时间',差别之处在于版本号。
测试环境:

select version();
5.7.16

生产环境:

select version();
5.7.21-20-log

带着关键字timestamp default CURRENT_TIMESTAMPColumn 'inserttime' cannot be null搜索Google。找到MySQL案例一则

测试环境:

show variables like '%explicit_defaults_for_timestamp%';
explicit_defaults_for_timestamp=OFF

生产环境:

show variables like '%explicit_defaults_for_timestamp%';
explicit_defaults_for_timestamp=ON

建表语句的意思是insert数据时,如果 inserttime 数据为空,则用系统default时间,这也是测试环境没有发现这个问题的原因。
生产环境里,explicit_defaults_for_timestamp=ON,即如果 inserttime 数据为空,则报错Column 'inserttime' cannot be null。注意:可以直接通过客户端如 dataGrip insert 一条 inserttime is null 的数据。

解决方法

  1. 修改生产参数配置,一来不建议,二来没有修改权限,需要联系DBA执行(还说服他们):
    set explicit_defaults_for_timestamp=OFF
  2. mybatis使用insertSelective而不用insert,建议:
<insert id="insert" parameterType="com.aaa.model.VarMonitor">
    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
        SELECT LAST_INSERT_ID()
    </selectKey>
    insert into var_monitor (var_id, inserttime, insertby, updatetime, updateby)
    values (#{varId,jdbcType=INTEGER}, #{inserttime,jdbcType=TIMESTAMP}, #{insertby,jdbcType=VARCHAR}, #{updatetime,jdbcType=TIMESTAMP}, #{updateby,jdbcType=VARCHAR})
</insert>

<insert id="insertSelective" parameterType="com.aaa.model.VarMonitor">
   <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
       SELECT LAST_INSERT_ID()
   </selectKey>
   insert into var_monitor
   <trim prefix="(" suffix=")" suffixOverrides=",">
       <if test="varId != null">
           var_id,
       </if>
       <if test="inserttime != null">
           inserttime,
       </if>
       <if test="insertby != null">
           insertby,
       </if>
       <if test="updatetime != null">
           updatetime,
       </if>
       <if test="updateby != null">
           updateby,
       </if>
   </trim>
   <trim prefix="values (" suffix=")" suffixOverrides=",">
       <if test="varId != null">
           #{varId,jdbcType=INTEGER},
       </if>
       <if test="inserttime != null">
           #{inserttime,jdbcType=TIMESTAMP},
       </if>
       <if test="insertby != null">
           #{insertby,jdbcType=VARCHAR},
       </if>
       <if test="updatetime != null">
           #{updatetime,jdbcType=TIMESTAMP},
       </if>
       <if test="updateby != null">
           #{updateby,jdbcType=VARCHAR},
       </if>
   </trim>
</insert>

  1. 修改DB设置,不建议:
    alter table var_monitor modify column inserttime timestamp default CURRENT_TIMESTAMP null comment '插入时间';

参考

MySQL案例一则

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/142264.html

(0)

相关推荐

  • 【数据库】MySQL知识合集

    导读:本篇文章讲解 【数据库】MySQL知识合集,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

    技术随笔 2023年2月22日
    00
  • ElasticJob分布式调度,监听器的使用附源码(四)

    导读:本篇文章讲解 ElasticJob分布式调度,监听器的使用附源码(四),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

    技术随笔 2023年2月17日
    00
  • 成功解决:Caused by: ParsingException[Failed to parse object: expecting token of type [START_OBJECT] but

    追求适度,才能走向成功;人在顶峰,迈步就是下坡;身在低谷,抬足既是登高;弦,绷得太紧会断;人,思虑过度会疯;水至清无鱼,人至真无友,山至高无树;适度,不是中庸,而是一种明智的生活态度。

    导读:本篇文章讲解 成功解决:Caused by: ParsingException[Failed to parse object: expecting token of type [START_OBJECT] but,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

    技术随笔 2023年4月9日
    00
  • Docker系列之Compose

    导读:本篇文章讲解 Docker系列之Compose,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

    技术随笔 2023年1月16日
    00
  • Java并发编程之CompletionService

    导读:本篇文章讲解 Java并发编程之CompletionService,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

    技术随笔 2023年1月19日
    00
  • java怎么连接数据库mysql

    导读:本篇文章讲解 java怎么连接数据库mysql,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

    技术随笔 2023年1月22日
    00
  • 【java基础】条件判断,if、switch

    有时候,不是因为你没有能力,也不是因为你缺少勇气,只是因为你付出的努力还太少,所以,成功便不会走向你。而你所需要做的,就是坚定你的梦想,你的目标,你的未来,然后以不达目的誓不罢休的那股劲,去付出你的努力,成功就会慢慢向你靠近。

    导读:本篇文章讲解 【java基础】条件判断,if、switch,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

    技术随笔 2023年5月29日
    00
  • Springboot集成Dubbo系列四:Linux搭建dubbo admin控制台

    导读:本篇文章讲解 Springboot集成Dubbo系列四:Linux搭建dubbo admin控制台,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

    技术随笔 2023年2月7日
    00
  • Spring Boot整合RabbitMQ-RabbitMQ

    导读:本篇文章讲解 Spring Boot整合RabbitMQ-RabbitMQ,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

    技术随笔 2023年2月13日
    00
  • Web前端笔记 — HTML③

    导读:本篇文章讲解 Web前端笔记 — HTML③,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

    也许你感觉自己的努力总是徒劳无功,但不必怀疑,你每天都离顶点更进一步。今天的你离顶点还遥遥无期。但你通过今天的努力,积蓄了明天勇攀高峰的力量。加油!

    技术随笔 2023年3月3日
    00

发表回复

登录后才能评论