문서의 이전 판입니다!
// ThreadLocal로 데이터 소스 구분자 보관 예제 public class CustomerContextHolder { private static final ThreadLocal<CustomerType> contextHolder = new ThreadLocal<CustomerType>(); public static void setCustomerType(CustomerType customerType) { Assert.notNull(customerType, "customerType cannot be null"); contextHolder.set(customerType); } public static CustomerType getCustomerType() { return (CustomerType) contextHolder.get(); } // 쓰레드가 종료될 때(특히 웹 애플리케이션의 경우 쓰레드 pool로 공유되기 때문에) 기존 데이터 clear가 필요하다. public static void clearCustomerType() { contextHolder.remove(); } }
@Transactional
이 걸리기 전)에 먼저 사용할 DB를 지정하는 작업이 선행돼야 한다.Propagation.REQUIRES_NEW/NEVER/NOT_SUPPORTED
등을 조합할 수 있어야 한다.