JAVA-2592: Update article on AbstractRoutingDatasource (#11320)

Co-authored-by: Dhawal Kapil <dhawalkapil@gmail.com>
This commit is contained in:
Rafael Lopez
2021-10-27 14:58:57 -04:00
committed by GitHub
parent 0afc8e7f25
commit f5734df43c
16 changed files with 217 additions and 17 deletions

View File

@@ -7,4 +7,5 @@
- [Resolving “Failed to Configure a DataSource” Error](https://www.baeldung.com/spring-boot-failed-to-configure-data-source)
- [Hibernate Field Naming with Spring Boot](https://www.baeldung.com/hibernate-field-naming-spring-boot)
- [Spring Boot with Hibernate](https://www.baeldung.com/spring-boot-hibernate)
- [A Guide to Spring AbstractRoutingDatasource](https://www.baeldung.com/spring-abstract-routing-data-source)
- More articles: [[more -->]](../spring-boot-persistence-2)

View File

@@ -0,0 +1,28 @@
package com.baeldung.dsrouting;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
/**
* Database access code for datasource routing example.
*/
public class ClientDao {
private static final String SQL_GET_CLIENT_NAME = "select name from client";
private final JdbcTemplate jdbcTemplate;
public ClientDao(DataSource datasource) {
this.jdbcTemplate = new JdbcTemplate(datasource);
}
public String getClientName() {
return this.jdbcTemplate.query(SQL_GET_CLIENT_NAME, rowMapper).get(0);
}
private static RowMapper<String> rowMapper = (rs, rowNum) -> {
return rs.getString("name");
};
}

View File

@@ -0,0 +1,27 @@
package com.baeldung.dsrouting;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
/**
* Returns thread bound client lookup key for current context.
*/
public class ClientDataSourceRouter extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return ClientDatabaseContextHolder.getClientDatabase();
}
public void initDatasource(DataSource clientADataSource,
DataSource clientBDataSource) {
Map<Object, Object> dataSourceMap = new HashMap<>();
dataSourceMap.put(ClientDatabase.CLIENT_A, clientADataSource);
dataSourceMap.put(ClientDatabase.CLIENT_A, clientBDataSource);
this.setTargetDataSources(dataSourceMap);
this.setDefaultTargetDataSource(clientADataSource);
}
}

View File

@@ -0,0 +1,7 @@
package com.baeldung.dsrouting;
public enum ClientDatabase {
CLIENT_A, CLIENT_B
}

View File

@@ -0,0 +1,26 @@
package com.baeldung.dsrouting;
import org.springframework.util.Assert;
/**
* Thread shared context to point to the datasource which should be used. This
* enables context switches between different clients.
*/
public class ClientDatabaseContextHolder {
private static final ThreadLocal<ClientDatabase> CONTEXT = new ThreadLocal<>();
public static void set(ClientDatabase clientDatabase) {
Assert.notNull(clientDatabase, "clientDatabase cannot be null");
CONTEXT.set(clientDatabase);
}
public static ClientDatabase getClientDatabase() {
return CONTEXT.get();
}
public static void clear() {
CONTEXT.remove();
}
}

View File

@@ -0,0 +1,21 @@
package com.baeldung.dsrouting;
/**
* Service layer code for datasource routing example. Here, the service methods are responsible
* for setting and clearing the context.
*/
public class ClientService {
private final ClientDao clientDao;
public ClientService(ClientDao clientDao) {
this.clientDao = clientDao;
}
public String getClientName(ClientDatabase clientDb) {
ClientDatabaseContextHolder.set(clientDb);
String clientName = this.clientDao.getClientName();
ClientDatabaseContextHolder.clear();
return clientName;
}
}

View File

@@ -0,0 +1,28 @@
package com.baeldung.dsrouting.model;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "client-a.datasource")
public class ClientADetails {
private String name;
private String script;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getScript() {
return script;
}
public void setScript(String script) {
this.script = script;
}
}

View File

@@ -0,0 +1,28 @@
package com.baeldung.dsrouting.model;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "client-b.datasource")
public class ClientBDetails {
private String name;
private String script;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getScript() {
return script;
}
public void setScript(String script) {
this.script = script;
}
}

View File

@@ -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");
}
}

View File

@@ -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();
}
}

View File

@@ -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");
}
}

View File

@@ -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();
}
}

View File

@@ -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

View File

@@ -0,0 +1,5 @@
create table client (
id numeric,
name varchar(50),
constraint pk_client primary key (id)
);