学习记录:@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);
    }

}

 

posted @ 2019-11-03 21:37  陈苏乾  阅读(381)  评论(0编辑  收藏  举报