How to secure frequent DataSource switches for AbstractRoutingDataSource?

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);
//<another user will switch customer type here to CustomerType.SILVER in another request>
List<Item> goldItems = catalog.getItems();

- Spring MVC? CustomerContextHolder.setCustomerType() ? synchronizeOnSession=true.

, , ?

.

+3
1

- Spring MVC?

, Spring MVC, ( ).

ClientContextHolder.setCustomerType() -?

. A ThreadLocal . javadoc:

. , , ( get set), , . ThreadLocal , (, ).

set ThreadLocal .

+5

Source: https://habr.com/ru/post/1750178/


All Articles