Home Java javaTutorial Spring's solution for dynamically switching multiple data sources

Spring's solution for dynamically switching multiple data sources

Jan 24, 2017 am 10:44 AM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? Apr 19, 2025 pm 09:51 PM

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

See all articles