java - How to make safe frequent DataSource switches for AbstractRoutingDataSource? -


I have implemented dynamic datosource routing for speeding, as in hibernation. I have several databases with the same structure and I have to choose which DB will run each specific query.

Everything works fine on the local host, but I worry about how it will keep in the real web site environment. To determine which data source they are using some fixed reference holders:

  Public class CustomerContextHolder {Private Static Last Threadlock & lt; Customer Type & gt; Referhold = New Thread Local & lt; Customer Type & gt; (); Public static void customer customer type (customer type customer type) {Assert.notNull (customerType, "customer type may not be nonsense"); ContextHolder.set (customerType); } Public stable customer type getCustomerType () {returns (customer type) reference holder.get (); } Public static zero empty customer type () {contextHolder.remove (); }}  

This is wrapped in some threadlocks containers, but what does this mean? What happens when two web requests are said in parallel to this piece of code:

  CustomerContextHolder.setCustomerType (CustomerType.GOLD); // & amp; Lt; Other users will switch the customer type from customer type. Silver in another request & gt; & Lt; Item & gt; Gold list = catalog.getItems ();  

Does every web request wrap in its own thread in Spring MVC? Will CustomerContextHolder.setCustomerType () changes be visible to other web users? My controllers have synchronizeOnSession = true .

To ensure that no data source will switch unless I run the required query for the current user

thanks.

Does every web request wrapped in my own thread in Spring MVC?

Yes, but its spring is nothing to do with MVC, the container is doing this (the container has a thread pool and selects one of them to handle each request) .

Will the customer containholder.Set customer custom type () changes be visible to other web users?

No A is by local definition for a thread. From Javadoc:

This class provides thread-local variables. These variables are different from their normal counterparts, each thread that enters through a (its get or set method), its own independent initial The copy is of the variable Threadlocal Example Typically, those sections contain private static fields, which want to associate the state with a thread (such as a User ID or Transaction ID).

You are not visible to other threads in a thread local in set . You should be OK


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -