Spring Transaction propagation REQUIRED, REQUIRES_NEW

后端 未结 3 629
梦谈多话
梦谈多话 2021-01-07 06:08

in following code method doService1() update correct sql but doService2() sql has some issue , but when i call doService() it has to c

3条回答
  •  清酒与你
    2021-01-07 06:36

    According to spring documentation(Check section 10.5.6.1),the spring framework transaction will Rollback for only RunTimeException.
    Not for other checked excpetions like SqlException. So if you really want a rollback for this exception you have to specify it like below

    @Transactional(propagation=Propagation.REQUIRES_NEW,rollbackFor=SQLException.class)
    public void doService2(){
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        String sql = "  update aa set a_name = 'hhh' where a_id = 4 and " ;
        int rowCount1 =  jdbcTemplate.update(sql);
        System.out.println(" rowCount2 >" + rowCount1);
    }
    

    Try this and let me know if it works.
    Also this might help more.

提交回复
热议问题