Data Source Switching
First of all, the important part of the xml file configured when two databases are required.
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"> </property> <property name="url" value="jdbc:sqlserver://192.168.0.1:1433; DatabaseName=A"> </property> <property name="username" value="sa"></property> <property name="password" value="123456"></property> </bean> <bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"> </property> <property name="url" value="jdbc:sqlserver://192.168.0.2:1433;databaseName=B"> </property> <property name="username" value="sa"></property> <property name="password" value="123456"></property> </bean> <bean id="dynamicDataSource" class="com.key.util.DynamicDataSource" > <property name="targetDataSources"> <map key-type="java.lang.String"> <entry value-ref="dataSource" key="dataSource"></entry> <entry value-ref="dataSource2" key="dataSource2"></entry> </map> </property> <property name="defaultTargetDataSource" ref="dataSource" > </property> </bean>
DataSource and performance2 are different data sources used in my project. The id is the default data source configured for dynamicDataSource.
// Classes to be used
Public class CustomerContextHolder {public static final String DATA_SOURCE_A = "dataSource"; public static final String DATA_SOURCE_ B = "cece2 "; private static final ThreadLocal <String> contextHolder = new ThreadLocal <String> (); public static void setCustomerType (String customerType) {contextHolder. set (customerType); // different objects accessed by each thread} public static String getCustomerType () {return contextHolder. get ();} public static void clearCustomerType () {contextHolder. remove ();}}
The source code for ThreadLocal is as follows:
public void set(T value) { Thread t = Thread.currentThread(); ThreadLocalMap map = getMap(t); if (map != null) map.set(this, value); else createMap(t, value); }
/** * Get the map associated with a ThreadLocal. Overridden in * InheritableThreadLocal. * * @param t the current thread * @return the map */ ThreadLocalMap getMap(Thread t) { return t.threadLocals; } /** * Create the map associated with a ThreadLocal. Overridden in * InheritableThreadLocal. * * @param t the current thread * @param firstValue value for the initial entry of the map */ void createMap(Thread t, T firstValue) { t.threadLocals = new ThreadLocalMap(this, firstValue); }
/** * Set the value associated with key. * @param key the thread local object * @param value the value to be set */ private void set(ThreadLocal<?> key, Object value) { // We don't use a fast path as with get() because it is at // least as common to use set() to create new entries as // it is to replace existing ones, in which case, a fast // path would fail more often than not. Entry[] tab = table; int len = tab.length; int i = key.threadLocalHashCode & (len-1); for (Entry e = tab[i]; e != null; e = tab[i = nextIndex(i, len)]) { ThreadLocal<?> k = e.get(); if (k == key) { e.value = value; return; } if (k == null) { replaceStaleEntry(key, value, i); return; } } tab[i] = new Entry(key, value); int sz = ++size; if (!cleanSomeSlots(i, sz) && sz >= threshold) rehash(); }
The usage is as follows:
Public String getList () {CustomerContextHolder. setCustomerType (CustomerContextHolder. DATA_SOURCE_ B); // switch to the data source TestService ccsqService = SpringUtil. getApplicationContext (). getBean (TestService. class); List <Ccsq> list = ccsqService. getCcsqList (); CustomerContextHolder. clearCustomerType (); // clear, return to the default data source result = JSONArray. fromObject (list); return SUCCESS ;}
So far, this is probably the use of dual-Data Source switching ~