개발 중 insert, update 쿼리 중 Exception 이 발생 하였지만 쿼리 요청이 rollback 되지않는 현상 정리

 

1. 액션

 - insert 혹은 update 문이 false 로 떨어질 경우 throw new Exception 을 처리 함

if(!dao.updateBl("updateQuery", dto)){
	throw new BaseException(SystemException.DB_NOT_UPDATE_ACTION);
}

 

2. 기대동작

 - RuntimeException 을 상속 받은 Exception 처리를 했으니 @Transactional 에 의해 rollback 처리를 기대함

 

3. 증상

 - DB update 가 rollback 되지 않고 정상적으로 업데이트가 됨

 

4. 원인 정리

 - update 문을 감싸는 try-catch 문을 신경쓰지 않았으며, catch 문에 처리되는 Exception 이 throw new Exception(e.getMessage()) 만 타고 있었음

 - Exception() 의 경우 checked exception 이므로 rollback 처리가 안됨

 

5. 해결방안

- 첫번째 방법 : try-catch 문의 Exception을 다음과 같이 변경 (RuntimeException 상속받은 클래스)

} catch (Exception e) {
	throw new BaseException(e.getMessage());
}

 - 두번째 방법 : throw new Exception() 으로 처리하고 @Transactional 의 옵션값을 보완함

@Transactional(rollbackFor = {RuntimeException.class, Exception.class})

 

현재 첫번째 방법으로 처리 했습니다.

 

 

참고 : cheese10yun.github.io/checked-exception/

+ Recent posts