이미 현업에서는 자주 사용하지만 따로 정리하지는 않아서 이번기회에 정식 레퍼런스를 보면서 내용을 찾아보았다
스프링 트랜잭션 처리
The @Transactional annotation is metadata that specifies that an interface, class, or method must have transactional semantics (for example, "start a brand new read-only transaction when this method is invoked, suspending any existing transaction"). The default @Transactional settings are as follows:
- The propagation setting is PROPAGATION_REQUIRED.
- The isolation level is ISOLATION_DEFAULT.
- The transaction is read-write.
- The transaction timeout defaults to the default timeout of the underlying transaction system, or to none if timeouts are not supported.
- Any RuntimeException triggers rollback, and any checked Exception does not
스프링 트랜잭션 처리중에 예외가 발생했을때 명시적으로 아래의 옵션들을 사용해서 롤백 여부를 결정할수 있다
rollbackFor | 롤백을 실행시키는 예외 클래스 목록들 |
rollbackForClassName | 롤백을 실행시키는 예외 클래스 이름들 |
noRollbackFor | 롤백을 실행시키지 않는 예외 클랙스 목록들 |
noRollbackForClassName | 롤백을 실행시키지 않는 예외 클래스 이름들 |
자바 vs 스프링 트랙잭션 처리에 대한 오해
구글에서 자바 트랜잭션 처리로 검색해 보면 Checked vs Unchecekd exception 에 대해 비교하고 unchecekd exception 에 대해 rollback 을 시행한다고 적혀있는 글을 많이 볼수있다.
이는 염언히 틀린말이다. 자바에서는 기존벅으로 트랜잭션에 대한 기본적인 처리 메커니즘을 제공하지 않으므로 Checked 이든 Unchecked 이든 프로그래머가 어떻게 프로그램을 짜느냐에 달려있다. 즉 Unchecked 에 롤백하는 메커니즘은 스프링 프레임워크를 사용할때 적용되는 기본 세팅이지 다른 프레임워크 혹은 프레임워크를 사용하지 않고 순수 자바 SDK 에서 제공하는 인터페이스로만 DB처리를 할경우 맞지 않는 말이된다.
이는 자바=스프링을 동일시하는데 발생해서 불러오는 자바 개발자들 사이에서 불러오는 흔한 오해라고 생각한다.
'Java > Spring' 카테고리의 다른 글
Spring 에서 런타임 환경에서 프로퍼티 값을 변경하는 방법 (0) | 2021.12.18 |
---|---|
스프링 프로파일 (Spring profile) 을 통해 환경별로 다른 설정을 해보자 (0) | 2016.11.24 |
Spring 내에서 리소스 불러오기 (0) | 2016.11.18 |
스프링 Properties 파일을 이용해서 설정하기 (0) | 2016.11.18 |
Mybatis + Myabtis Spring 사용시 마이바티스 설정파일 사용법 (0) | 2016.11.14 |