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);
        }
    
    }
  • 相关阅读:
    BZOJ 1057 悬线法求最大01矩阵
    POJ 2248
    SPOJ
    51NOD
    2017-2018 ACM-ICPC, NEERC, Moscow Subregional Contest J. Judging the Trick
    POJ 1379 模拟退火
    POJ 2420 模拟退火
    Frontend 事后诸葛亮
    【Frontend】Alpha Review 展示博客
    ASE19 团队项目 alpha 阶段 Frontend 组 scrum5 记录
  • 原文地址:https://www.cnblogs.com/chensuqian/p/11789302.html
Copyright © 2011-2022 走看看