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);
        }
    
    }
  • 相关阅读:
    2012619 win7环境Jooma建站手记解决问题
    linux学习(4)ubuntu开机自动挂载nfs服务器上的home分区
    linux学习(3)nginx tomcat集群
    oracle to_date 函数
    inux学习(2)vim设置
    linux学习(1)常用命令入门
    tomcat域名绑定
    tomcat内存溢出问题解决思路
    TreeView控件无限级 一次性加载和异步加载剖析
    数据查询支持中文拼音首字母模糊检索
  • 原文地址:https://www.cnblogs.com/chensuqian/p/11789302.html
Copyright © 2011-2022 走看看