Spring Boot Multi data sources and their transaction management configuration methods

  • 2020-06-19 10:23:54
  • OfStack

The preparatory work

We start by adding Spring-ES4en dependencies and driver dependencies that need to access the database to our project.

The configuration file


spring.datasource.prod.driverClassName=com.mysql.jdbc.Driver
spring.datasource.prod.url=jdbc:mysql://127.0.0.1:3306/prod
spring.datasource.prod.username=root
spring.datasource.prod.password=123456

spring.datasource.dev.driverClassName=com.mysql.jdbc.Driver
spring.datasource.dev.url=jdbc:mysql://127.0.0.1:3306/dev
spring.datasource.dev.username=root
spring.datasource.dev.password=123456

JavaConfig

Start by creating the Java configuration class and annotating it @Configuration .


@Configuration
public class JdbcConfig {

}

Configure the data source

Add one of the data sources @Primary . Because Spring Boot Jdbc USES dataSource transparently during its automatic configuration for developers, Spring Boot will not be able to determine which Datasource implementation class to use when there are two.

When we do not specify a name for @Bean, Spring defaults to the method name as Bean's name, so the following two data sources are named respectively prodDataSource and devDataSource .


@Bean
@Primary
@ConfigurationProperties(prefix = "spring.datasource.prod")
public DataSource prodDataSource(){
 return DataSourceBuilder.create().build();
}

@Bean
@ConfigurationProperties(prefix = "spring.datasource.dev")
public DataSource devDataSource(){
 return DataSourceBuilder.create().build();
}

The property names in the configuration file do not need to be written spring.datasource.xxx Omega in the form of omega a.b.c.url No problem, as long as the prefix is specified when configuring bean a.b.c

Configuration JdbcTemplate

Here we return no JdbcTemplate , but its implementation interface JdbcOperations .

The participation of Bean, Spring, will automatically inject its corresponding Bean, so we do not need the corresponding Bean of @Autowired for reuse here.

Spring associates the variable name with the name of Bean. Here the name of the reference data source matches the method name of the above data source Bean, so there is no need to specify which Bean is using the @Qualifier annotation.


@Bean
public JdbcOperations prodJdbcOperations(DataSource prodDataSource) {
 return new JdbcTemplate(prodDataSource);
}

@Bean
public JdbcOperations devJdbcOperations(DataSource devDataSource) {
 return new JdbcTemplate(devDataSource);
}

use

Direct injection JdbcOperations Can be

As noted above, no @Primary0 And so on.


 @Autowired
 private JdbcOperations devJdbcOperations;

 @Autowired
 private JdbcOperations prodJdbcOperations;

Transaction configuration

Enable transaction management

In the project entry class, add the following annotations to enable transaction management.


@EnableTransactionManagement

Configure the transaction manager


@Bean
public PlatformTransactionManager prodTransactionManager(DataSource prodDataSource) {
 return new DataSourceTransactionManager(prodDataSource);
}

@Bean
public PlatformTransactionManager devTransactionManager(DataSource sitDataSource) {
 return new DataSourceTransactionManager(sitDataSource);
}

use

When used, simply add annotations to the methods that require transactions @Transactional , and specify its value value. Again, the value value matches the corresponding method name.


@Transactional(value = "prodTransactionManager")
public void prod() {
 prodJdbcOperations.queryForList("SELECT * FROM USER");
}

@Transactional(value = "devTransactionManager")
public void dev() {
 devJdbcOperations.queryForList("SELECT * FROM USER");
}

Note that @Transactional configuration transactions have many restrictions, such as method must be public, method calls with an annotated method transaction not taking effect if the same class does not have the annotation. Place such as the annotations can also configure the interface class, please refer to the specific usage Spring official document corresponding chapters http: / / docs spring. io/spring - framework docs / 4.2 x/spring - framework - reference/html/transaction html


Related articles: