SoFunction
Updated on 2025-03-03

How to access different databases in SpringBoot

In Spring Boot applications, accessing different databases often involves multi-data source configuration. Here is how to configure and manage multiple data sources in Spring Boot:

1. Use the multi-data source configuration officially supported by Spring Boot

Through the above steps and precautions, multiple data sources can be successfully configured and managed in Spring Boot applications to meet different database access needs.

Add dependencies

Make sure to be inSpring Boot Starter JDBC and database driver dependencies are added to the file. For example, for MySQL, the following dependencies need to be added:

<dependency>  
    <groupId></groupId>  
    <artifactId>spring-boot-starter-jdbc</artifactId>  
</dependency>  
<dependency>  
    <groupId>mysql</groupId>  
    <artifactId>mysql-connector-java</artifactId>  
</dependency>

2. Configure the data source

existorConfigure information about multiple data sources in the file. For example:

# Primary DataSource  
=jdbc:mysql://localhost:3306/db1  
=root  
=root  
-class-name=  

# Secondary DataSource  
=jdbc:mysql://localhost:3306/db2  
=root  
=root  
-class-name=

3. Create a data source configuration class

use@ConfigurationAnnotation creates a data source configuration class and defines multiple data sources there. For example:

@Configuration  
public class DataSourceConfig {  

    @Primary  
    @Bean(name = "primaryDataSource")  
    @ConfigurationProperties(prefix = "")  
    public DataSource primaryDataSource() {  
        return ().build();  
    }  

    @Bean(name = "secondDataSource")  
    @ConfigurationProperties(prefix = "-datasource")  
    public DataSource secondDataSource() {  
        return ().build();  
    }  
}

4. Use data sources in Service

In a Service that requires the use of the data source,@Autowiredand@QualifierAnnotation specifies the specific data source. For example:

@Service  
public class UserServiceImpl implements UserService {  

    @Autowired  
    @Qualifier("primaryDataSource")  
    private DataSource primaryDataSource;  

    @Autowired  
    @Qualifier("secondDataSource")  
    private DataSource secondDataSource;  

    // ... Methods to use data source}

2. Use third-party libraries to implement multiple data sources

In addition to using the multi-data source configuration officially supported by Spring Boot, some open source third-party libraries can also be used to implement multi-data source configuration, such as HikariCP, Druid and other connection pools.

Add dependencies

existAdd dependencies for third-party connection pools to the file. For example, for Druid:

&lt;dependency&gt;  
    &lt;groupId&gt;&lt;/groupId&gt;  
    &lt;artifactId&gt;druid-spring-boot-starter&lt;/artifactId&gt;  
    &lt;version&gt;Version number&lt;/version&gt;  
&lt;/dependency&gt;

Configure data source

existorThe information about the data source configuration in the file is similar to the official configuration, but the prefix may be different. For example:

# Primary DataSource  
=jdbc:mysql://localhost:3306/db1  
=root  
=root  
-class-name=  

# Secondary DataSource  
=jdbc:mysql://localhost:3306/db2  
=root  
=root  
-class-name=

Create a data source configuration class

In the data source configuration class, use a third-party connection pool to create a data source. For example:

Using data sources in Service

Similar to the official configuration, used in Service@Autowiredand@QualifierAnnotation specifies the specific data source.

@Configuration  
public class DataSourceConfig {  

    @Bean(name = "primaryDataSource")  
    @ConfigurationProperties(prefix = "")  
    public DataSource primaryDataSource() {  
        return new DruidDataSource();  
    }  

    @Bean(name = "secondDataSource")  
    @ConfigurationProperties(prefix = "")  
    public DataSource secondDataSource() {  
        return new DruidDataSource();  
    }  
}

3. Things to note

Transaction Management

When using multiple data sources, transaction management issues need to be considered. You can use Spring's transaction management function, through@TransactionalAnnotations to manage transactions. However, it should be noted that transactions between different data sources are independent and cannot be managed across data sources.

Data synchronization

If there is a requirement for data synchronization between multiple data sources, a corresponding synchronization mechanism needs to be implemented. It can be done using the database's own replication function, or by writing synchronization scripts.

Performance optimization

When using multiple data sources, you need to pay attention to performance optimization issues. Performance can be improved through connection pool configuration, SQL statement optimization, etc.

Configuration information correctness

It is necessary to ensure that the configuration information of each data source is correct, including URL, username, password, driver class name, etc. If the configuration information is incorrect, it will cause problems such as connection failure or data conflict.

This is the end of this article about how SpringBoot accesses different databases. For more related SpringBoot to access different database content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!