Skip to content

Commit 3bb538a

Browse files
chrisoberlemaibin
authored andcommitted
BAEL-1314 Guide to AbstractRoutingDatasource (eugenp#2989)
* BAEL-1314 initial import. Guide to AbstractRoutingDatasource. * BAEL-1314 Guide to Spring AbstractRoutingDatasource * update test name and rename test classes * move dsrouting example to spring-boot project. rename db create script.
1 parent a38e6e2 commit 3bb538a

8 files changed

Lines changed: 228 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.baeldung.dsrouting;
2+
3+
import javax.sql.DataSource;
4+
5+
import org.springframework.jdbc.core.JdbcTemplate;
6+
import org.springframework.jdbc.core.RowMapper;
7+
8+
/**
9+
* Database access code for datasource routing example.
10+
*/
11+
public class ClientDao {
12+
13+
private static final String SQL_GET_CLIENT_NAME = "select name from client";
14+
15+
private final JdbcTemplate jdbcTemplate;
16+
17+
public ClientDao(DataSource datasource) {
18+
this.jdbcTemplate = new JdbcTemplate(datasource);
19+
}
20+
21+
public String getClientName() {
22+
return this.jdbcTemplate.query(SQL_GET_CLIENT_NAME, rowMapper)
23+
.get(0);
24+
}
25+
26+
private static RowMapper<String> rowMapper = (rs, rowNum) -> {
27+
return rs.getString("name");
28+
};
29+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.baeldung.dsrouting;
2+
3+
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
4+
5+
/**
6+
* Returns thread bound client lookup key for current context.
7+
*/
8+
public class ClientDataSourceRouter extends AbstractRoutingDataSource {
9+
10+
@Override
11+
protected Object determineCurrentLookupKey() {
12+
return ClientDatabaseContextHolder.getClientDatabase();
13+
}
14+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.baeldung.dsrouting;
2+
3+
public enum ClientDatabase {
4+
5+
ACME_WIDGETS, WIDGETS_ARE_US, WIDGET_DEPOT
6+
7+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.baeldung.dsrouting;
2+
3+
import org.springframework.util.Assert;
4+
5+
/**
6+
* Thread shared context to point to the datasource which should be used. This
7+
* enables context switches between different clients.
8+
*/
9+
public class ClientDatabaseContextHolder {
10+
11+
private static final ThreadLocal<ClientDatabase> CONTEXT = new ThreadLocal<>();
12+
13+
public static void set(ClientDatabase clientDatabase) {
14+
Assert.notNull(clientDatabase, "clientDatabase cannot be null");
15+
CONTEXT.set(clientDatabase);
16+
}
17+
18+
public static ClientDatabase getClientDatabase() {
19+
return CONTEXT.get();
20+
}
21+
22+
public static void clear() {
23+
CONTEXT.remove();
24+
}
25+
26+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.baeldung.dsrouting;
2+
3+
/**
4+
* Service layer code for datasource routing example. Here, the service methods are responsible
5+
* for setting and clearing the context.
6+
*/
7+
public class ClientService {
8+
9+
private final ClientDao clientDao;
10+
11+
public ClientService(ClientDao clientDao) {
12+
this.clientDao = clientDao;
13+
}
14+
15+
public String getClientName(ClientDatabase clientDb) {
16+
ClientDatabaseContextHolder.set(clientDb);
17+
String clientName = this.clientDao.getClientName();
18+
ClientDatabaseContextHolder.clear();
19+
return clientName;
20+
}
21+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package org.baeldung.dsrouting;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import javax.sql.DataSource;
7+
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.context.annotation.Configuration;
10+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
11+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
12+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
13+
14+
@Configuration
15+
public class DataSourceRoutingTestConfiguration {
16+
17+
@Bean
18+
public ClientService clientService() {
19+
return new ClientService(new ClientDao(clientDatasource()));
20+
}
21+
22+
@Bean
23+
public DataSource clientDatasource() {
24+
Map<Object, Object> targetDataSources = new HashMap<>();
25+
DataSource acmeWidgetsDatasource = acmeWidgetsDatasource();
26+
DataSource widgetsAreUsDatasource = widgetsAreUsDatasource();
27+
DataSource widgetsDepotDatasource = widgetsDepotDatasource();
28+
targetDataSources.put(ClientDatabase.ACME_WIDGETS, acmeWidgetsDatasource);
29+
targetDataSources.put(ClientDatabase.WIDGETS_ARE_US, widgetsAreUsDatasource);
30+
targetDataSources.put(ClientDatabase.WIDGET_DEPOT, widgetsDepotDatasource);
31+
32+
ClientDataSourceRouter clientRoutingDatasource = new ClientDataSourceRouter();
33+
clientRoutingDatasource.setTargetDataSources(targetDataSources);
34+
clientRoutingDatasource.setDefaultTargetDataSource(acmeWidgetsDatasource);
35+
return clientRoutingDatasource;
36+
}
37+
38+
private DataSource acmeWidgetsDatasource() {
39+
EmbeddedDatabaseBuilder dbBuilder = new EmbeddedDatabaseBuilder();
40+
EmbeddedDatabase embeddedDb = dbBuilder.setType(EmbeddedDatabaseType.H2)
41+
.setName("ACMEWIDGETS")
42+
.addScript("classpath:dsrouting-db.sql")
43+
.build();
44+
return embeddedDb;
45+
}
46+
47+
private DataSource widgetsAreUsDatasource() {
48+
EmbeddedDatabaseBuilder dbBuilder = new EmbeddedDatabaseBuilder();
49+
EmbeddedDatabase embeddedDb = dbBuilder.setType(EmbeddedDatabaseType.H2)
50+
.setName("WIDGETSAREUS")
51+
.addScript("classpath:dsrouting-db.sql")
52+
.build();
53+
return embeddedDb;
54+
}
55+
56+
private DataSource widgetsDepotDatasource() {
57+
EmbeddedDatabaseBuilder dbBuilder = new EmbeddedDatabaseBuilder();
58+
EmbeddedDatabase embeddedDb = dbBuilder.setType(EmbeddedDatabaseType.H2)
59+
.setName("WIDGETDEPOT")
60+
.addScript("classpath:dsrouting-db.sql")
61+
.build();
62+
return embeddedDb;
63+
}
64+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.baeldung.dsrouting;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import javax.sql.DataSource;
6+
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.jdbc.core.JdbcTemplate;
12+
import org.springframework.test.context.ContextConfiguration;
13+
import org.springframework.test.context.junit4.SpringRunner;
14+
15+
@RunWith(SpringRunner.class)
16+
@ContextConfiguration(classes = DataSourceRoutingTestConfiguration.class)
17+
public class DataSourceRoutingTests {
18+
19+
@Autowired
20+
DataSource routingDatasource;
21+
22+
@Autowired
23+
ClientService clientService;
24+
25+
@Before
26+
public void setup() {
27+
final String SQL_ACME_WIDGETS = "insert into client (id, name) values (1, 'ACME WIDGETS')";
28+
final String SQL_WIDGETS_ARE_US = "insert into client (id, name) values (2, 'WIDGETS ARE US')";
29+
final String SQL_WIDGET_DEPOT = "insert into client (id, name) values (3, 'WIDGET DEPOT')";
30+
31+
JdbcTemplate jdbcTemplate = new JdbcTemplate();
32+
jdbcTemplate.setDataSource(routingDatasource);
33+
34+
ClientDatabaseContextHolder.set(ClientDatabase.ACME_WIDGETS);
35+
jdbcTemplate.execute(SQL_ACME_WIDGETS);
36+
ClientDatabaseContextHolder.clear();
37+
38+
ClientDatabaseContextHolder.set(ClientDatabase.WIDGETS_ARE_US);
39+
jdbcTemplate.execute(SQL_WIDGETS_ARE_US);
40+
ClientDatabaseContextHolder.clear();
41+
42+
ClientDatabaseContextHolder.set(ClientDatabase.WIDGET_DEPOT);
43+
jdbcTemplate.execute(SQL_WIDGET_DEPOT);
44+
ClientDatabaseContextHolder.clear();
45+
}
46+
47+
@Test
48+
public void givenClientDbs_whenContextsSwitch_thenRouteToCorrectDatabase() throws Exception {
49+
50+
// test ACME WIDGETS
51+
String clientName = clientService.getClientName(ClientDatabase.ACME_WIDGETS);
52+
assertEquals(clientName, "ACME WIDGETS");
53+
54+
// test WIDGETS_ARE_US
55+
clientName = clientService.getClientName(ClientDatabase.WIDGETS_ARE_US);
56+
assertEquals(clientName, "WIDGETS ARE US");
57+
58+
// test WIDGET_DEPOT
59+
clientName = clientService.getClientName(ClientDatabase.WIDGET_DEPOT);
60+
assertEquals(clientName, "WIDGET DEPOT");
61+
}
62+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
create table client (
2+
id numeric,
3+
name varchar(50),
4+
constraint pk_client primary key (id)
5+
);

0 commit comments

Comments
 (0)