2012-10-11 3 views

답변

10

RuntimeExceptionrollbackFor 목록에 포함 할 필요가 없습니다. 비록 당신이 그것을 언급하지 않더라도 그것을 처리 할 것입니다.

나는 JdbcTemplate을 위해 그것을 밖으로 시도했다 : -

@Transactional(propagation = Propagation.REQUIRED, rollbackFor = MyException.class) 
public void updateSalary(final int increment){ 
    jdbcTemplate.update("update EMPLOYEE set emp_salary = emp_salary + ?", increment); 
    throw new RuntimeException("update exception"); 
} 
그러나
 
Output: 
After Insertion: 
1 Deepak 35000 
2 Yogesh 35000 
3 Aditya 35000 

update exception 
After Update 
1 Deepak 35000 
2 Yogesh 35000 
3 Aditya 35000 
+0

가 어쨌든 커밋하지 않음 당신은 readOnly = true를 가지고 있기 때문에? –

+0

실수를 지적하기위한 @AlexBeardsley thnx ... 응답을 수정했습니다. 코드를 다시 실행했습니다. 그렇습니다. 롤백 목록에 런타임 예외를 포함 할 필요가 없습니다. – deepakraut

5

, Spring 프레임 워크의 트랜잭션 인프라 코드가 기본적으로 만 에 대한 트랜잭션을 표시합니다 점에 유의하시기 바랍니다 런타임시 롤백, 검사되지 않은 예외; 즉, throw 된 예외는 RuntimeException의 인스턴스 또는 서브 클래스입니다. (오류는 기본적으로 롤백이됩니다.) 트랜잭션 방식에서 발생하는 예외는 롤백중인 트랜잭션에서 이되지 않습니다.

Source

이 도움이 될 수 있습니다 : Spring transaction management with checked and unchecked exception

0

은 그래서 (기본적으로 RuntimeException의)뿐만 아니라 다시 CheckedException 롤, 예 :

@Transactional(rollbackFor = Exception.class) 
public void save(Book book) throws Exception { 
    bookRepository.save(book); 
    System.out.println("Saved in transcation."); 
    // No data is persisted 
    if (true) { 
     throw new Exception(); 
    } 
} 
관련 문제