이미 현업에서는 자주 사용하지만 따로 정리하지는 않아서 이번기회에 정식 레퍼런스를 보면서 내용을 찾아보았다

스프링 트랜잭션 처리

https://docs.spring.io/spring-framework/docs/current/reference/html/data-access.html#transaction-declarative-attransactional-settings

 

Data Access

The Data Access Object (DAO) support in Spring is aimed at making it easy to work with data access technologies (such as JDBC, Hibernate, or JPA) in a consistent way. This lets you switch between the aforementioned persistence technologies fairly easily, a

docs.spring.io

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처리를 할경우 맞지 않는 말이된다. 

 

이는 자바=스프링을 동일시하는데 발생해서 불러오는 자바 개발자들 사이에서 불러오는 흔한 오해라고 생각한다.

+ Recent posts