Spring transaction rollback

Sometimes things Spring things do not roll back attention points

Suppose the methodA function calls the methodB method in addition to its own business logic

 

1 Want to roll back an exception thrown in try catch (default is runtime exception)

2 增加 @Transactional(readOnly = false,rollbackFor = Exception.class)

3 Both methods must throw exceptions, either 1 catch and throw themselves, 2 or throw upwards

 

/***
     * The first business sql
     */
    @Transactional(readOnly = false,rollbackFor = Exception.class)
    public void methodA() {
        try {
            //update modify SQL1 business
            String a = "UPDATE TABLE1 SET FirstName = 'Fred ' WHERE LastName = 'Wilson'";
           
            this.methodB();//Modify SQL2
        } catch (Exception e) {
            throw new RuntimeException();//Throw exception
        }
    }

 

 

    /**
     * Second business sql
     */
    public void methodB() {
        try {
            // update modify SQL2 business
            String b = "UPDATE TABLE2 SET FirstName = 'Fred' WHERE LastName = 'Wilson'";
        } catch (Exception e ) {
            throw new RuntimeException(e);//throw exception
        }
    }

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326770312&siteId=291194637