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);
        }
    
    }
  • 相关阅读:
    面向对象先导课感想
    【LATEX】个人版latex论文模板
    【前端】wangEditor(富文本编辑器) 简易使用示例
    【前端】ACE Editor(代码编辑器) 简易使用示例
    苦果:像专家一样思考,像外行一样实践
    Matplotlib cheatsheet
    版本控制最佳实践cheatsheet
    “左手程序员、右手作家”Jupyter Notebook Cheatsheet
    Pandas DataWrangling cheatsheet(数据整理 )
    Numpy basic sheatsheet
  • 原文地址:https://www.cnblogs.com/chensuqian/p/11789302.html
Copyright © 2011-2022 走看看