Home > Java > javaTutorial > body text

Spring's solution for dynamically switching multiple data sources

高洛峰
Release: 2017-01-24 10:44:20
Original
1712 people have browsed it

Preface

Spring dynamically configures multiple data sources, that is, segmenting data in large applications and using multiple database instances for management, which can effectively improve the horizontal scalability of the system. Such a solution will be different from the common single data instance solution, which requires the program to dynamically decide which database instance to store the data in and which database to extract the data from during runtime based on the current request and system status.

Spring2.x and later versions adopt the Proxy mode, which means we implement a virtual data source in the solution and use it to encapsulate the data source selection logic, so that the data source selection logic can be effectively transferred from separated from the Client. The Client provides the context required for selection (because this is what the Client knows), and the virtual DataSource implements the selection of the data source based on the context provided by the Client.

Implementation

The specific implementation is that the virtual DataSource only needs to inherit AbstractRoutingDataSource to implement determineCurrentLookupKey() in which the selection logic of the data source is encapsulated.

1. Dynamic configuration of multiple data sources

1. Data source name constant class:

/**
 * 动态配置多数据源
 * 数据源的名称常量类
 * @author LONGHUI_LUO
 *
 */
public class DataSourceConst {
 public static final String TEST="test";
 public static final String USER="User";
}
Copy after login


2. Create a class to obtain and set the context environment, mainly responsible for changing the name of the context data source:

/**
 * 获得和设置上下文环境 主要负责改变上下文数据源的名称
 *
 * @author LONGHUI_LUO
 *
 */
public class DataSourceContextHolder {
 private static final ThreadLocal contextHolder = new ThreadLocal(); // 线程本地环境
  
 // 设置数据源类型
 public static void setDataSourceType(String dataSourceType) {
  contextHolder.set(dataSourceType);
 }
  
 // 获取数据源类型
 public static String getDataSourceType() {
  return (String) contextHolder.get();
 }
  
 // 清除数据源类型
 public static void clearDataSourceType() {
  contextHolder.remove();
 }
  
}
Copy after login


3 . Create a dynamic data source class. Note that this class must inherit AbstractRoutingDataSource and implement the method determineCurrentLookupKey. This method returns an Object, usually a string:

/**
 * 建立动态数据源
 *
 * @author LONGHUI_LUO
 *
 */
public class DynamicDataSource extends AbstractRoutingDataSource {
  
 protected Object determineCurrentLookupKey() {
 // 在进行DAO操作前,通过上下文环境变量,获得数据源的类型
 return DataSourceContextHolder.getDataSourceType();
 }
  
}
Copy after login


4. Write spring configuration files to configure multiple data sources

  <!-- 数据源相同的内容 -->
<bean
  id="parentDataSource"
  class="org.apache.commons.dbcp.BasicDataSource"
  destroy-method="close">
  <property
   name="driverClassName"
   value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
  <property name="username" value="sa" />
  <property name="password" value="net2com" />
</bean>
<!-- start以下配置各个数据源的特性 -->
<bean parent="parentDataSource" id="testDataSource">
  <propertynamepropertyname="url" value="jdbc:sqlserver://localhost:1433;databaseName=test" />
</bean>
<bean parent="parentDataSource" id="UserDataSource">
   <property
   name="url"
   value="jdbc:sqlserver://localhost:1433;databaseName=User" />
</bean>
<!-- end 配置各个数据源的特性 -->
Copy after login


5. Write spring configuration files to configure multiple data source mapping relationships

<bean class="com.xxxx.datasouce.DynamicDataSource" id="dataSource">
 <property name="targetDataSources">
  <map key-type="java.lang.String">
   <entry value-ref="testDataSource" key="test"></entry>
   <entry value-ref="UserDataSource" key="User"></entry>
  </map>
 </property>
 <property name="defaultTargetDataSource" ref="testDataSource" ></property>
</bean>
Copy after login


In this configuration, the first property attribute configures the target data source, must be the same type as the value in the static key-value comparison class DataSourceConst; the value of key in must be the same as the static key The value is the same as the value in the control class. If there are multiple values, you can configure multiple < entry> tags. The second property attribute configures the default data source.

Dynamic switching is the data source

DataSourceContextHolder.setDataSourceType(DataSourceConst.TEST);
Copy after login


##The advantages of this solution

First of all, this solution is completely Solved under the spring framework, the data source is still configured in the spring configuration file, and the sessionFactory still configures its dataSource attribute. It does not even know the change of dataSource. The only difference is that a MultiDataSource is added between the real dataSource and sessionFactory.


Secondly, it is simple to implement and easy to maintain. Although I have said so many things about this plan, it is actually all analysis. The only code we really need to write is MultiDataSource and SpObserver. The only two methods that really need to be written in the MultiDataSource class are getDataSource() and getDataSource(sp), while the SpObserver class is even simpler. The simpler the implementation, the smaller the possibility of errors and the higher the maintainability.


Finally, this solution can make single data source compatible with multiple data sources. This solution does not affect the writing of BUS and DAO at all. If our project is developed with a single data source at the beginning and needs to be changed to multiple data sources as the project progresses, we only need to modify the spring configuration and slightly modify the MVC layer to write the required data in the request. Data source name, the change is completed. If our project wants to change back to a single data source, we only need to simply modify the configuration file. This will add more flexibility to our project.


Disadvantages of this solution

It cannot solve the problem of sharing the "dataSource" variable when multiple users access the singleton "sessionFactory", resulting in the result of contention for "dataSource", which is essentially similar to The "producer-consumer" problem in operating systems. Therefore, when accessed by multiple users, multiple data sources may lead to reduced system performance.

Summary

The above is the entire content of this article. I hope the content of this article can bring some help to everyone's study or work. If you have any questions, you can leave a message to communicate.

For more articles related to Spring’s solution for dynamically switching multiple data sources, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template