zoukankan      html  css  js  c++  java
  • 学习记录:@Transactional 事务不生效

    测试时使用spring boot2.2.0,在主类中调用,@Transactional 不起作用,原代码如下:

    @SpringBootApplication
    @Slf4j
    @Component
    public class Chapter08TransactionStatementApplication implements CommandLineRunner {
    
        @Autowired
        private JdbcTemplate jdbcTemplate;
    
        public static void main(String[] args) {
            SpringApplication.run(Chapter08TransactionStatementApplication.class, args);
        }
    
        @Override
        public void run(String... args) throws Exception {
            log.info("before insert:count:" + getCount());
            insert();
            log.info("after insert:count:" + getCount());
            log.info("before insertThenRollback:count:" + getCount());
            try {
                insertThenRollback();
            } catch (Exception e) {
                log.info(e.toString());
            }
            log.info("after insertThenRollback:count:" + getCount());
            log.info("before invokeInsertThemRollback:count:" + getCount());
            try {
                invokeInsertThemRollback();
            } catch (Exception e) {
                log.info(e.toString());
            }
            log.info("after invokeInsertThemRollback:count:" + getCount());
    
        }
    
        @Transactional
        public void insert() {
            CallTask callTask = new CallTask(UUID.randomUUID().toString().replace("-", ""), 0, 1, UUID.randomUUID().toString().replace("-", ""), new Date(),
                    UUID.randomUUID().toString().replace("-", ""), new Date(), UUID.randomUUID().toString().replace("-", ""));
            jdbcTemplate.update("insert into call_task values(?,?,?,?,?,?,?,?)", callTask.getId(), callTask.getType(), callTask.getStatus(), callTask.getDataKey(),
                    callTask.getCreateTime(), callTask.getCreateUserId(), callTask.getUpdateTime(), callTask.getUpdateUserId());
    
        }
    
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
        @Transactional(rollbackFor = RollbackException.class)
        public void insertThenRollback() throws RollbackException {
            CallTask callTask = new CallTask(UUID.randomUUID().toString().replace("-", ""), 0, 1, UUID.randomUUID().toString().replace("-", ""), new Date(),
                    UUID.randomUUID().toString().replace("-", ""), new Date(), UUID.randomUUID().toString().replace("-", ""));
    //        jdbcTemplate.update("insert into call_task values(?,?,?,?,?,?,?,?)", callTask.getId(), callTask.getType(), callTask.getStatus(), callTask.getDataKey(),
    //                callTask.getCreateTime(), callTask.getCreateUserId(), callTask.getUpdateTime(), callTask.getUpdateUserId());
            jdbcTemplate.execute("insert into call_task values('" + callTask.getId() + "',0,0,'" + callTask.getDataKey() + "','" + format.format(new Date()) + "','"
                    + callTask.getCreateUserId() + "','" + format.format(new Date()) + "','" + callTask.getUpdateUserId() + "')");
            throw new RollbackException();
        }
    
        public void invokeInsertThemRollback() throws RollbackException {
            insertThenRollback();
        }
    
        private int getCount() {
            return jdbcTemplate.queryForObject("select count(*) from call_task", Integer.class);
        }
    }
    

      修改为使用serivce调用即可(访问修饰符必须为:public):

    @Service
    public class CallTaskServiceImpl implements CallTaskService {
    
        @Autowired
        private JdbcTemplate jdbcTemplate;
    
        @Override
        @Transactional
        public void insert() {
            CallTask callTask = new CallTask(UUID.randomUUID().toString().replace("-", ""), 0, 1, UUID.randomUUID().toString().replace("-", ""), new Date(),
                    UUID.randomUUID().toString().replace("-", ""), new Date(), UUID.randomUUID().toString().replace("-", ""));
            jdbcTemplate.update("insert into call_task values(?,?,?,?,?,?,?,?)", callTask.getId(), callTask.getType(), callTask.getStatus(), callTask.getDataKey(),
                    callTask.getCreateTime(), callTask.getCreateUserId(), callTask.getUpdateTime(), callTask.getUpdateUserId());
    
        }
    
        @Override
        @Transactional(rollbackFor = RollbackException.class)
        public void insertThenRollback() throws RollbackException {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            CallTask callTask = new CallTask(UUID.randomUUID().toString().replace("-", ""), 0, 1, UUID.randomUUID().toString().replace("-", ""), new Date(),
                    UUID.randomUUID().toString().replace("-", ""), new Date(), UUID.randomUUID().toString().replace("-", ""));
    //        jdbcTemplate.update("insert into call_task values(?,?,?,?,?,?,?,?)", callTask.getId(), callTask.getType(), callTask.getStatus(), callTask.getDataKey(),
    //                callTask.getCreateTime(), callTask.getCreateUserId(), callTask.getUpdateTime(), callTask.getUpdateUserId());
            jdbcTemplate.execute("insert into call_task values('" + callTask.getId() + "',0,0,'" + callTask.getDataKey() + "','" + format.format(new Date()) + "','"
                    + callTask.getCreateUserId() + "','" + format.format(new Date()) + "','" + callTask.getUpdateUserId() + "')");
            throw new RollbackException();
        }
    
        @Override
        public void invokeInsertThemRollback() throws RollbackException {
            insertThenRollback();
        }
    
        @Override
        public int getCount() {
            return jdbcTemplate.queryForObject("select count(*) from call_task", Integer.class);
        }
    
    }
  • 相关阅读:
    Cactus详细讲解
    jexus处理静态文件(处理后缀)
    简单的html兼容(参考js和css的常规写法)
    IOHelper(自制常用的输入输出的帮助类)
    @Html.AntiForgeryToken() 源码分析,表单防伪码的生成
    缓存处理类(MemoryCache结合文件缓存)
    MVC过滤大法(过滤静态文件)
    C#文件监控工具(对追加内容的监控并输出)
    C#在Linux上的开发指南
    对java面试文章的技术漫谈的C#技术理解
  • 原文地址:https://www.cnblogs.com/chensuqian/p/11789302.html
Copyright © 2011-2022 走看看