JAVA-2592: Update article on AbstractRoutingDatasource (#11320)
Co-authored-by: Dhawal Kapil <dhawalkapil@gmail.com>
This commit is contained in:
+55
@@ -0,0 +1,55 @@
|
||||
package com.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.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = DataSourceRoutingTestConfiguration.class)
|
||||
@DirtiesContext
|
||||
public class DataSourceRoutingIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
DataSource routingDatasource;
|
||||
|
||||
@Autowired
|
||||
ClientService clientService;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
final String SQL_CLIENT_A = "insert into client (id, name) values (1, 'CLIENT A')";
|
||||
final String SQL_CLIENT_B = "insert into client (id, name) values (2, 'CLIENT B')";
|
||||
|
||||
JdbcTemplate jdbcTemplate = new JdbcTemplate();
|
||||
jdbcTemplate.setDataSource(routingDatasource);
|
||||
|
||||
ClientDatabaseContextHolder.set(ClientDatabase.CLIENT_A);
|
||||
jdbcTemplate.execute(SQL_CLIENT_A);
|
||||
ClientDatabaseContextHolder.clear();
|
||||
|
||||
ClientDatabaseContextHolder.set(ClientDatabase.CLIENT_B);
|
||||
jdbcTemplate.execute(SQL_CLIENT_B);
|
||||
ClientDatabaseContextHolder.clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClientDbs_whenContextsSwitch_thenRouteToCorrectDatabase() throws Exception {
|
||||
|
||||
// test ACME WIDGETS
|
||||
String clientName = clientService.getClientName(ClientDatabase.CLIENT_A);
|
||||
assertEquals(clientName, "CLIENT A");
|
||||
|
||||
// test WIDGETS_ARE_US
|
||||
clientName = clientService.getClientName(ClientDatabase.CLIENT_B);
|
||||
assertEquals(clientName, "CLIENT B");
|
||||
}
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
package com.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.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 clientADatasource = clientADatasource();
|
||||
DataSource clientBDatasource = clientBDatasource();
|
||||
targetDataSources.put(ClientDatabase.CLIENT_A, clientADatasource);
|
||||
targetDataSources.put(ClientDatabase.CLIENT_B, clientBDatasource);
|
||||
|
||||
ClientDataSourceRouter clientRoutingDatasource = new ClientDataSourceRouter();
|
||||
clientRoutingDatasource.setTargetDataSources(targetDataSources);
|
||||
clientRoutingDatasource.setDefaultTargetDataSource(clientADatasource);
|
||||
return clientRoutingDatasource;
|
||||
}
|
||||
|
||||
private DataSource clientADatasource() {
|
||||
EmbeddedDatabaseBuilder dbBuilder = new EmbeddedDatabaseBuilder();
|
||||
return dbBuilder.setType(EmbeddedDatabaseType.H2).setName("CLIENT_A").addScript("dsrouting-db.sql").build();
|
||||
}
|
||||
|
||||
private DataSource clientBDatasource() {
|
||||
EmbeddedDatabaseBuilder dbBuilder = new EmbeddedDatabaseBuilder();
|
||||
return dbBuilder.setType(EmbeddedDatabaseType.H2).setName("CLIENT_B").addScript("dsrouting-db.sql").build();
|
||||
}
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
package com.baeldung.dsrouting;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import com.baeldung.dsrouting.model.ClientADetails;
|
||||
import com.baeldung.dsrouting.model.ClientBDetails;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(
|
||||
classes = {ClientADetails.class, ClientBDetails.class})
|
||||
@ContextConfiguration(classes = SpringBootDataSourceRoutingTestConfiguration.class)
|
||||
@DirtiesContext
|
||||
@EnableConfigurationProperties(ClientBDetails.class)
|
||||
public class SpringBootDataSourceRoutingIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
DataSource routingDatasource;
|
||||
|
||||
@Autowired
|
||||
ClientService clientService;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
final String SQL_CLIENT_A = "insert into client (id, name) values (1, 'CLIENT A')";
|
||||
final String SQL_CLIENT_B = "insert into client (id, name) values (2, 'CLIENT B')";
|
||||
|
||||
JdbcTemplate jdbcTemplate = new JdbcTemplate();
|
||||
jdbcTemplate.setDataSource(routingDatasource);
|
||||
|
||||
ClientDatabaseContextHolder.set(ClientDatabase.CLIENT_A);
|
||||
jdbcTemplate.execute(SQL_CLIENT_A);
|
||||
ClientDatabaseContextHolder.clear();
|
||||
|
||||
ClientDatabaseContextHolder.set(ClientDatabase.CLIENT_B);
|
||||
jdbcTemplate.execute(SQL_CLIENT_B);
|
||||
ClientDatabaseContextHolder.clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenClientDbs_whenContextsSwitch_thenRouteToCorrectDatabase() throws Exception {
|
||||
|
||||
// test ACME WIDGETS
|
||||
String clientName = clientService.getClientName(ClientDatabase.CLIENT_A);
|
||||
assertEquals(clientName, "CLIENT A");
|
||||
|
||||
// test WIDGETS_ARE_US
|
||||
clientName = clientService.getClientName(ClientDatabase.CLIENT_B);
|
||||
assertEquals(clientName, "CLIENT B");
|
||||
}
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
package com.baeldung.dsrouting;
|
||||
|
||||
import com.baeldung.dsrouting.model.ClientADetails;
|
||||
import com.baeldung.dsrouting.model.ClientBDetails;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.*;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Configuration
|
||||
public class SpringBootDataSourceRoutingTestConfiguration {
|
||||
@Autowired
|
||||
private ClientADetails clientADetails;
|
||||
@Autowired
|
||||
private ClientBDetails clientBDetails;
|
||||
|
||||
@Bean
|
||||
public ClientService clientService() {
|
||||
return new ClientService(new ClientDao(clientDatasource()));
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DataSource clientDatasource() {
|
||||
Map<Object, Object> targetDataSources = new HashMap<>();
|
||||
DataSource clientADatasource = clientADatasource();
|
||||
DataSource clientBDatasource = clientBDatasource();
|
||||
targetDataSources.put(ClientDatabase.CLIENT_A, clientADatasource);
|
||||
targetDataSources.put(ClientDatabase.CLIENT_B, clientBDatasource);
|
||||
|
||||
ClientDataSourceRouter clientRoutingDatasource = new ClientDataSourceRouter();
|
||||
clientRoutingDatasource.setTargetDataSources(targetDataSources);
|
||||
clientRoutingDatasource.setDefaultTargetDataSource(clientADatasource);
|
||||
return clientRoutingDatasource;
|
||||
}
|
||||
|
||||
private DataSource clientADatasource() {
|
||||
EmbeddedDatabaseBuilder dbBuilder = new EmbeddedDatabaseBuilder();
|
||||
return dbBuilder.setType(EmbeddedDatabaseType.H2)
|
||||
.setName(clientADetails.getName())
|
||||
.addScript(clientADetails.getScript())
|
||||
.build();
|
||||
}
|
||||
|
||||
private DataSource clientBDatasource() {
|
||||
EmbeddedDatabaseBuilder dbBuilder = new EmbeddedDatabaseBuilder();
|
||||
return dbBuilder.setType(EmbeddedDatabaseType.H2)
|
||||
.setName(clientBDetails.getName())
|
||||
.addScript(clientBDetails.getScript())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,14 @@ spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=sa
|
||||
|
||||
#database details for CLIENT_A
|
||||
client-a.datasource.name=CLIENT_A
|
||||
client-a.datasource.script=dsrouting-db.sql
|
||||
|
||||
#database details for CLIENT_B
|
||||
client-b.datasource.name=CLIENT_B
|
||||
client-b.datasource.script=dsrouting-db.sql
|
||||
|
||||
# hibernate.X
|
||||
hibernate.dialect=org.hibernate.dialect.H2Dialect
|
||||
hibernate.show_sql=true
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
create table client (
|
||||
id numeric,
|
||||
name varchar(50),
|
||||
constraint pk_client primary key (id)
|
||||
);
|
||||
Reference in New Issue
Block a user