zoukankan      html  css  js  c++  java
  • mybatis update数据时无异常但没更新成功;update异常时如数据超出大小限制,造成死锁

    没更新的问题原因:

    sqlSession.commit();

    没执行commit,但官方文档里有这样的描述:“默认情况下 MyBatis 不会自动提交事务,除非它侦测到有插入、更新或删除操作改变了数据库。”  

    源码:

    <update id="updateTest" parameterType="cn.td.user.TestModel"> //TestModel是一个JavaBean
     update test_table set test_case_suc_num = #{test_case_suc_num} where test_name = #{test_name} </update>
        public void updateAllInfo(List<TestModel> list) throws IOException{     
            SqlSessionFactory sqlSessionFactory = getSqlFactory();
            SqlSession sqlSession = sqlSessionFactory.openSession();
            for(TestModel testModel : list){
                System.out.println(testModel);
                sqlSession.update("cn.td.dao.updatetest",testModel);
                sqlSession.commit();
            }
            sqlSession.close();
        }

     以上的java代码中update异常会造成  数据库死锁,导致下次无法正常更新。

    死锁的概念就是类似git中的lock,操作残留 或者 互斥。

    解决办法:

    public void updateAllInfo(List<TestModel> list) throws IOException{
            SqlSessionFactory sqlSessionFactory = getSqlFactory();
            SqlSession sqlSession = sqlSessionFactory.openSession();
            for(TestModel testModel : list){
                System.out.println(testModel);
    
                try {
                    sqlSession.update("cn.td.dao.updatetest",testModel);
                } catch (Exception e) {
                    sqlSession.rollback();
                    e.printStackTrace();
                }
                
                sqlSession.commit();
            }
            sqlSession.close();
        }

    事务回滚。

  • 相关阅读:
    图片懒加载
    文字表情转换成小图标
    页面跳页面的参数获取
    vue v-for里面再套v-if和v-esle
    滚动条样式的修改
    vue-cil生产环境和发布环境的配置
    css隐藏滚动条并且可以滑动
    vue-cli脚手架一些插件安装elementui和axios
    闭包
    定时器、运动、日历
  • 原文地址:https://www.cnblogs.com/dongzhuangdian/p/9446407.html
Copyright © 2011-2022 走看看