BAEL-1314 Guide to AbstractRoutingDatasource (#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.
This commit is contained in:
+64
@@ -0,0 +1,64 @@
|
||||
package org.baeldung.dsrouting;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||
|
||||
@Configuration
|
||||
public class DataSourceRoutingTestConfiguration {
|
||||
|
||||
@Bean
|
||||
public ClientService clientService() {
|
||||
return new ClientService(new ClientDao(clientDatasource()));
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DataSource clientDatasource() {
|
||||
Map<Object, Object> targetDataSources = new HashMap<>();
|
||||
DataSource acmeWidgetsDatasource = acmeWidgetsDatasource();
|
||||
DataSource widgetsAreUsDatasource = widgetsAreUsDatasource();
|
||||
DataSource widgetsDepotDatasource = widgetsDepotDatasource();
|
||||
targetDataSources.put(ClientDatabase.ACME_WIDGETS, acmeWidgetsDatasource);
|
||||
targetDataSources.put(ClientDatabase.WIDGETS_ARE_US, widgetsAreUsDatasource);
|
||||
targetDataSources.put(ClientDatabase.WIDGET_DEPOT, widgetsDepotDatasource);
|
||||
|
||||
ClientDataSourceRouter clientRoutingDatasource = new ClientDataSourceRouter();
|
||||
clientRoutingDatasource.setTargetDataSources(targetDataSources);
|
||||
clientRoutingDatasource.setDefaultTargetDataSource(acmeWidgetsDatasource);
|
||||
return clientRoutingDatasource;
|
||||
}
|
||||
|
||||
private DataSource acmeWidgetsDatasource() {
|
||||
EmbeddedDatabaseBuilder dbBuilder = new EmbeddedDatabaseBuilder();
|
||||
EmbeddedDatabase embeddedDb = dbBuilder.setType(EmbeddedDatabaseType.H2)
|
||||
.setName("ACMEWIDGETS")
|
||||
.addScript("classpath:dsrouting-db.sql")
|
||||
.build();
|
||||
return embeddedDb;
|
||||
}
|
||||
|
||||
private DataSource widgetsAreUsDatasource() {
|
||||
EmbeddedDatabaseBuilder dbBuilder = new EmbeddedDatabaseBuilder();
|
||||
EmbeddedDatabase embeddedDb = dbBuilder.setType(EmbeddedDatabaseType.H2)
|
||||
.setName("WIDGETSAREUS")
|
||||
.addScript("classpath:dsrouting-db.sql")
|
||||
.build();
|
||||
return embeddedDb;
|
||||
}
|
||||
|
||||
private DataSource widgetsDepotDatasource() {
|
||||
EmbeddedDatabaseBuilder dbBuilder = new EmbeddedDatabaseBuilder();
|
||||
EmbeddedDatabase embeddedDb = dbBuilder.setType(EmbeddedDatabaseType.H2)
|
||||
.setName("WIDGETDEPOT")
|
||||
.addScript("classpath:dsrouting-db.sql")
|
||||
.build();
|
||||
return embeddedDb;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package org.baeldung.dsrouting;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = DataSourceRoutingTestConfiguration.class)
|
||||
public class DataSourceRoutingTests {
|
||||
|
||||
@Autowired
|
||||
DataSource routingDatasource;
|
||||
|
||||
@Autowired
|
||||
ClientService clientService;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
final String SQL_ACME_WIDGETS = "insert into client (id, name) values (1, 'ACME WIDGETS')";
|
||||
final String SQL_WIDGETS_ARE_US = "insert into client (id, name) values (2, 'WIDGETS ARE US')";
|
||||
final String SQL_WIDGET_DEPOT = "insert into client (id, name) values (3, 'WIDGET DEPOT')";
|
||||
|
||||
JdbcTemplate jdbcTemplate = new JdbcTemplate();
|
||||
jdbcTemplate.setDataSource(routingDatasource);
|
||||
|
||||
ClientDatabaseContextHolder.set(ClientDatabase.ACME_WIDGETS);
|
||||
jdbcTemplate.execute(SQL_ACME_WIDGETS);
|
||||
ClientDatabaseContextHolder.clear();
|
||||
|
||||
ClientDatabaseContextHolder.set(ClientDatabase.WIDGETS_ARE_US);
|
||||
jdbcTemplate.execute(SQL_WIDGETS_ARE_US);
|
||||
ClientDatabaseContextHolder.clear();
|
||||
|
||||
ClientDatabaseContextHolder.set(ClientDatabase.WIDGET_DEPOT);
|
||||
jdbcTemplate.execute(SQL_WIDGET_DEPOT);
|
||||
ClientDatabaseContextHolder.clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClientDbs_whenContextsSwitch_thenRouteToCorrectDatabase() throws Exception {
|
||||
|
||||
// test ACME WIDGETS
|
||||
String clientName = clientService.getClientName(ClientDatabase.ACME_WIDGETS);
|
||||
assertEquals(clientName, "ACME WIDGETS");
|
||||
|
||||
// test WIDGETS_ARE_US
|
||||
clientName = clientService.getClientName(ClientDatabase.WIDGETS_ARE_US);
|
||||
assertEquals(clientName, "WIDGETS ARE US");
|
||||
|
||||
// test WIDGET_DEPOT
|
||||
clientName = clientService.getClientName(ClientDatabase.WIDGET_DEPOT);
|
||||
assertEquals(clientName, "WIDGET DEPOT");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user