this article에 따라 Spring + Hibernate 용 Dynamic DataSource Routing을 구현했습니다. 같은 구조의 여러 데이터베이스가 있고 각 특정 쿼리를 실행할 db를 선택해야합니다.AbstractRoutingDataSource에 대해 안전한 데이터 소스 스위치를 자주 만드는 방법은 무엇입니까?
모든 것이 로컬 호스트에서 제대로 작동하지만 실제 웹 사이트 환경에서 이것이 어떻게 유지 될지 걱정됩니다. 그들은 사용할 데이터 소스를 결정하기 위해 몇 가지 정적 컨텍스트 홀더를 사용하고 있습니다 :
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();
}
public static void clearCustomerType() {
contextHolder.remove();
}
}
그것은 일부의 ThreadLocal 용기 내부에 싸여있다,하지만 정확히 무엇을 의미 하는가?
CustomerContextHolder.setCustomerType(CustomerType.GOLD);
//<another user will switch customer type here to CustomerType.SILVER in another request>
List<Item> goldItems = catalog.getItems();
스프링 MVC에서 자체 스레드에 랩 모든 웹 요청입니다 : 두 개의 웹 요청이 병렬로이 코드 조각을 호출 할 때 어떻게됩니까? CustomerContextHolder.setCustomerType()
변경 사항이 다른 웹 사용자에게 표시됩니까? 내 컨트롤러는 synchronizeOnSession=true
입니다.
현재 사용자에게 필요한 쿼리를 실행할 때까지 아무도 데이터 소스를 전환하지 않도록하려면 어떻게해야합니까?
감사합니다.
나는 괜찮을 것이라고 생각한다. 각 서블릿 요청은 단일 스레드에서 실행됩니다 (그리고 Spring은 바로 밑의 서블릿입니다). 최대 절전 모드 샤드 사용을 고려 했습니까? –