I applied Dynamic DataSource Routing for Spring + Hibernate in accordance with this article . I have several databases with the same structure, and I need to choose which db will run each specific request.
Everything works fine on localhost, but I'm worried about how this will depend on the actual website environment. They use some static context holder to determine which data source to use:
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();
}
}
It is wrapped inside a ThreadLocal container, but what exactly does that mean? What happens when two web requests are parallel to this piece of code:
CustomerContextHolder.setCustomerType(CustomerType.GOLD);
List<Item> goldItems = catalog.getItems();
- Spring MVC? CustomerContextHolder.setCustomerType() ? synchronizeOnSession=true.
, , ?
.