In modern applications, handling multiple data sources is a common requirement. It may be due to business needs, or in order to achieve read and write separation, we often need to configure multiple data sources in the same application and select different data sources according to specific operations. In Spring Boot, such requirements can be easily achieved through dynamic data sources. This blog will provide detailed information on how to configure and use dynamic data sources in Spring Boot and demonstrate how to switch to a specified data source.
1. Why use dynamic data sources?
Business isolation: In some complex business scenarios, different modules may need to connect to different databases. With dynamic data source configuration, these different database access can be isolated in the same application.
Read and write separation: To improve the performance of the system, read and write operations are usually separated into different databases. For example, place the write operation on the primary database and the read operation on the secondary database.
Database migration: During system migration, switching between two databases may be necessary to ensure smooth migration.
2. Multi-data source configuration in Spring Boot
We will use an example to illustrate how to configure and use dynamic data sources in Spring Boot. Suppose we have two data sources, one is master and the other is adcontrol. The master data source is mainly used for the main business database, while the adcontrol data source is used for relevant data storage for advertising control.
1. Data source configuration
First, we need to configure two data sources in the file.
datasource: dynamic: primary: master # Use the master library by default strict: false # Do not use strict mode datasource: master: url: jdbc:mysql://127.0.0.1:3306/test?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false username: root password: 123456 driver-class-name: hikari: connection-timeout: 30000 max-lifetime: 1800000 max-pool-size: 15 min-idle: 5 connection-test-query: select 1 pool-name: YsxHikariCP adcontrol: url: jdbc:mysql://ip:3306/test2?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false username: test password: test driver-class-name:
In the above configuration, the master data source is the default data source, and the adcontrol data source is the backup advertising control data source.
2. Introduce dynamic data source dependencies
In order for Spring Boot to identify and use these dynamic data sources, we need to introduce a dependency library for dynamic data sources. Here we use dynamic-datasource-spring-boot-starter open source library.
Add the following dependencies in :
<dependency> <groupId>-datasource</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>3.2.1</version> <!-- Make sure to use the right version --> </dependency>
3. Configure dynamic data sources
passDynamicDataSource
, we can flexibly switch data sources in our application. Here is a simple oneDynamicDataSource
Configuration example:
import ; import ; import ; import ; import ; import ; import ; @Configuration public class DataSourceConfig { @Bean public DataSource dataSource() { DynamicRoutingDataSource dataSource = new DynamicRoutingDataSource(); // Configure the main data source DataSource masterDataSource = () .url("jdbc:mysql://127.0.0.1:3306/test1?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false") .username("root") .password("123456") .driverClassName("") .build(); // Configure ad control data sources DataSource adcontrolDataSource = () .url("jdbc:mysql://ip1:3306/test2?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false") .username("test") .password("test") .driverClassName("") .build(); // Put the data source into the map Map<Object, Object> dataSourceMap = new HashMap<>(); ("master", masterDataSource); ("adcontrol", adcontrolDataSource); (masterDataSource); (dataSourceMap); return dataSource; } }
4. Dynamically switch data sources using @DS annotation
At the service level, we can use@DS
Annotation to specify which data source the current method should use. For example:
import ; import ; @Service public class MyService { @DS("adcontrol") public void handleAdControlLogic() { // Use adcontrol data source to perform operations } @DS("master") public void handleMasterLogic() { // Use master data source to perform operations } }
pass@DS
Annotation, we can flexibly switch data sources at the method level to ensure that different business logics use the correct database connection.
3. Exception handling and rollback
When using multiple data sources, it is important to handle transactions and exceptions well. If operations of multiple data sources are performed within a transaction, transaction management and exception handling become particularly critical.
Spring provides support for a global transaction manager that can be configured to manage transactions across multiple data sources. However, when using dynamic-datasource, special care should be taken to ensure that multiple data sources operating in the same transaction can be properly committed or rolled back.
For example:
import ; @Service public class MyService { @Transactional @DS("master") public void processTransaction() { try { // Operate the master database handleMasterLogic(); // Switch to adcontrol data source handleAdControlLogic(); } catch (Exception e) { // Handle exceptions, may need to roll back the transaction throw new RuntimeException("Transaction failed, rollback", e); } } @DS("adcontrol") public void handleAdControlLogic() { // Operate adcontrol database } @DS("master") public void handleMasterLogic() { // Operate the master database } }
In the above code, ifhandleAdControlLogic
If the method throws an exception, the entire transaction will be rolled back.
4. Frequently Asked Questions and Solutions
-
Data source switching failed
- make sure
@DS
The name pointed to by the annotation is exactly the same as the data source name in the configuration. - examine
DynamicDataSource
Whether it is configured correctly.
- make sure
-
Transaction rollback failed
- Ensure that all data sources used in the same transaction support transaction management.
- use
@Transactional
Annotations to manage transactions.
-
Performance issues
- Dynamic data source switching may result in additional performance overhead. Consider using a connection pool to optimize database connection management.
-
Unable to connect to the database
- Check the database connection configuration to ensure
url
、username
andpassword
The parameters are correct. - Check whether the database server is accessible.
- Check the database connection configuration to ensure
5. Summary
With dynamic data source configuration, Spring Boot applications can easily address the complex needs of multiple data sources. Whether it is business isolation, read and write separation, or database migration, dynamic data sources can provide flexible and efficient solutions.
In practical applications, rational planning and use of multiple data sources can significantly improve the scalability and reliability of the system. During the implementation process, special attention needs to be paid to transaction management and exception handling to ensure the consistency and integrity of the data.
Through this article, I believe you have mastered the basic methods of configuring and using dynamic data sources in Spring Boot. In actual projects, the use of dynamic data sources can be further optimized and expanded according to business needs. Hope this article can be helpful to you.
The above is the detailed explanation of the configuration and use of dynamic data sources in SpringBoot. For more information about SpringBoot dynamic data sources, please pay attention to my other related articles!