@EnableTransactionManagement
🔄 @EnableTransactionManagement
정의
@EnableTransactionManagement는 Spring에서 선언적 트랜잭션 관리를 활성화하는 설정 애너테이션이다.@Transactional이 실제로 동작하도록 트랜잭션 처리 기반을 만들어준다.
트랜잭션 프록시가 생성되어 메서드 단위로 트랜잭션 경계를 관리할 수 있다.
🧩 역할
- 선언적 트랜잭션 관리를 활성화한다
@Transactional이 작동할 수 있도록 프록시 기반 처리를 제공한다- JPA, JDBC, MyBatis 같은 데이터 접근 계층에서 공통적으로 사용할 수 있다
🛠 사용법
기본 설정
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableTransactionManagement
public class TransactionConfig {
}
@Transactional과 함께 사용
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class UserService {
@Transactional
public void createUser(User user) {
// 데이터베이스 트랜잭션 내에서 실행됨
}
}
⚠️ 주의사항
@Transactional은 보통 프록시를 통해 동작하므로 내부 메서드 호출에는 적용되지 않을 수 있다.public메서드 중심으로 사용하는 것이 일반적이다.- 이미 Spring Boot 환경에서는 관련 자동 설정이 포함되는 경우가 많아, 직접 선언이 필요한지 확인하는 것이 좋다.
- 트랜잭션 범위가 길어지면 성능과 락 점유 시간에 영향을 줄 수 있다.
📌 정리
@EnableTransactionManagement는 Spring의 선언적 트랜잭션 기능을 켜는 설정이다.@Transactional과 함께 사용해야 트랜잭션 경계가 제대로 적용된다.- 데이터 접근 기술이 달라도 공통적으로 활용할 수 있다.
댓글남기기