Skip to content

Commit 86462d7

Browse files
committed
multiple ds springboot
1 parent 1068302 commit 86462d7

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
---
2+
layout: post
3+
title: "Multiple Datasources with Spring Boot and Hibernate"
4+
date: 2019-10-12 12:00:00
5+
categories:
6+
---
7+
8+
Adding two database connection to a Spring Boot application is quite straightforward unless you are using `JpaRepositories`. Simple task of adding urls and giving JdbcTemplate different identifier names become an annoying task.
9+
10+
Starting with adding configurations of two different Datasource to the project:
11+
12+
{% highlight yaml %}
13+
app.datasource.backup.url=jdbc:mysql://backup_db
14+
app.datasource.backup.username=user
15+
app.datasource.backup.password=password
16+
app.datasource.backup.maximum-pool-size=10
17+
18+
app.datasource.main.url=jdbc:mysql://main_db
19+
app.datasource.main.username=user
20+
app.datasource.main.password=password
21+
app.datasource.main.maximum-pool-size=10
22+
{% endhighlight %}
23+
24+
These will be used for datasource creation initially and later identifiers will be enough.
25+
26+
{% highlight java %}
27+
@Bean
28+
@Primary
29+
@ConfigurationProperties("app.datasource.main")
30+
public DataSourceProperties mainDataSourceProperties() {
31+
return new DataSourceProperties();
32+
}
33+
34+
@Bean(name = "dbMain")
35+
@Primary
36+
public DataSource mainDataSource() {
37+
return mainDataSourceProperties().initializeDataSourceBuilder().build();
38+
}
39+
40+
@Bean(name = "jdbcMain")
41+
@Autowired
42+
public JdbcTemplate mainJdbcTemplate(@Qualifier("dbMain") DataSource dsMain) {
43+
return new JdbcTemplate(dsMain);
44+
}
45+
46+
47+
@Bean(name = "jdbcBackup")
48+
@Autowired
49+
public JdbcTemplate slaveJdbcTemplate(@Qualifier("dbBackup") DataSource dsSlave) {
50+
return new JdbcTemplate(dsSlave);
51+
}
52+
53+
@Bean
54+
@ConfigurationProperties("app.datasource.backup")
55+
public DataSourceProperties backupDataSourceProperties() {
56+
return new DataSourceProperties();
57+
}
58+
59+
60+
@Bean(name = "dbBackup")
61+
public DataSource backupDataSource() {
62+
return backupDataSourceProperties().initializeDataSourceBuilder().build();
63+
}
64+
{% endhighlight %}
65+
66+
In a configuration file or in the SpringBoot main class define these datasource beans.
67+
* Read configuration information for resources
68+
* Create a datasource with a name from that configuration
69+
* Set the datasource created to a JdbcTemplate. The name given here will be used for accessing to the different JdbcTemplate beans as `@Qualifier("jdbcMain") JdbcTemplate jdbcTemplate`
70+
71+
While defining a jdbc repository give the identifier to access a specific database.
72+
73+
{% highlight java %}
74+
@Repository
75+
public class GameRepository {
76+
77+
private final JdbcTemplate jdbcTemplate;
78+
79+
@Autowired
80+
public GameRepository(@Qualifier("jdbcMain") JdbcTemplate jdbcTemplate) {
81+
this.jdbcTemplate = jdbcTemplate;
82+
}
83+
...
84+
{% endhighlight %}
85+
86+
What if we want to use JPA and define our objects with `Entity`. Datasource definitions and initialization will not change. However different `EntityManagers` and `TransactionManagers` must be defined.

0 commit comments

Comments
 (0)