Spring boot with multiple database
When I developing my project requires to connect to the multiple databases. But I didn't get any better resources on the web. With the best of knowledge to giving the apparent tutorial. Let's dive into the code.
Step -1:- Define Database connection properties
For every Database, connection requires a URL, username, and password. These properties define in an application.properties file.
For example :
application.properties
#Connection one properties
spring.datasourceOne.url=jdbc:postgresql://xyz
spring.datasourceOne.username=ABC
spring.datasourceOne.password=ABC
spring.datasourceOne.driver-class-name=org.postgresql.Driver
#Connection two properties
spring.datasourceTwo.url=jdbc:postgresql://xyz
spring.datasourceTwo.username=ABC
spring.datasourceTwo.password=ABC
spring.datasourceTwo.driver-class-name=org.postgresql.Driver
Step-2:- Make a Connection to Databases
Once define database properties and its time to make a connection to the database with these properties by using DataSource.
Datasource is for keeping multiple connections, Transaction Management, and JDBC connectivity.
@Bean(name=”DSOneProp”)
@ConfigurationProperties(“spring.datasourceOne”)
public DataSourceProperties sampleDSProperties() {
return new DataSourceProperties();
}
@Bean(name=”DSOne”)
public DataSource sampleDS( @Qualifier(“DSOneProp”) DataSourceProperties sampleDSProperties) {
return sampleDSProperties.initializeDataSourceBuilder().build();
}
@Bean(name=”transEmFactOne”)
public LocalContainerEntityManagerFactoryBean sampleEmFact(@Qualifier(“DSOne”) DataSource DS,EntityManagerFactoryBuilder builder) {
return builder.dataSource(DS).packages(“com.xyz.sampleModels”).build();
}
@Bean
public PlatformTransactionManager transDSTransmanager(@Qualifier(“transEmFactOne”) EntityManagerFactory transEmFact) {
return new JpaTransactionManager(transEmFact);
}
@Bean(name = “sampleOneJdbcTemplate”)
public JdbcTemplate postgresJdbcTemplate( @Qualifier(“DSOne”)
DataSource dsPostgres) {
return new JdbcTemplate(dsPostgres);
}
This above code for only making a connection to only one database. For a different connection need to define different class.
Step-3:- Making a query
Querying a database by two ways, Repository and JDBC.
1. By querying with Repository, define the Repository and Models package in Configuration class.
For Example:-
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages= {“com.xyz .repositories”},entityManagerFactoryRef=”transEmFactOne”)
public class TransConfig {
@Primary
@Bean(name=”DSOneProp”)
@ConfigurationProperties(“spring.datasourceOne”)
public DataSourceProperties sampleDSProperties() {
return new DataSourceProperties();
}
@Primary
@Bean(name=”DSOne”)
public DataSource sampleDS( @Qualifier(“DSOneProp”) DataSourceProperties sampleDSProperties) {
return sampleDSProperties.initializeDataSourceBuilder().build();
}
@Primary
@Bean(name=”transEmFactOne”)
public LocalContainerEntityManagerFactoryBean sampleEmFact(@Qualifier(“DSOne”) DataSource DS,EntityManagerFactoryBuilder builder) {
return builder.dataSource(DS).packages(“com.xyz.sampleModels”).build();
}
@Primary
@Bean
public PlatformTransactionManager transDSTransmanager(@Qualifier(“transEmFactOne”) EntityManagerFactory transEmFact) {
return new JpaTransactionManager(transEmFact);
}
@Primary
@Bean(name = “sampleOneJdbcTemplate”)
public JdbcTemplate postgresJdbcTemplate( @Qualifier(“DSOne”)
DataSource dsPostgres) {
return new JdbcTemplate(dsPostgres);
}
}
2. By querying with JDBC, inject the JDBC Template class dependency into our class.
For Example:-
@Repository
public class SampleJDBCTemplate {
@Autowired
@Qualifier(“sampleOneJdbcTemplate”)
private JdbcTemplate jdbcTemplate;
public void JDBCQuery(){
//write to your JDBC query, jdbcTemplate.Query
}
}
Source: