JAVA-2592: Update article on AbstractRoutingDatasource (#11320)
Co-authored-by: Dhawal Kapil <dhawalkapil@gmail.com>
This commit is contained in:
parent
a41f46c594
commit
b9fadd8f3d
|
@ -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)
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -34,11 +34,11 @@ public class DataSourceRoutingTestConfiguration {
|
|||
|
||||
private DataSource clientADatasource() {
|
||||
EmbeddedDatabaseBuilder dbBuilder = new EmbeddedDatabaseBuilder();
|
||||
return dbBuilder.setType(EmbeddedDatabaseType.H2).setName("CLIENT_A").addScript("classpath:dsrouting-db.sql").build();
|
||||
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("classpath:dsrouting-db.sql").build();
|
||||
return dbBuilder.setType(EmbeddedDatabaseType.H2).setName("CLIENT_B").addScript("dsrouting-db.sql").build();
|
||||
}
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
);
|
|
@ -5,7 +5,7 @@
|
|||
- [JPA Pagination](https://www.baeldung.com/jpa-pagination)
|
||||
- [Sorting with JPA](https://www.baeldung.com/jpa-sort)
|
||||
- [Self-Contained Testing Using an In-Memory Database](https://www.baeldung.com/spring-jpa-test-in-memory-database)
|
||||
- [A Guide to Spring AbstractRoutingDatasource](https://www.baeldung.com/spring-abstract-routing-data-source)
|
||||
- [Obtaining Auto-generated Keys in Spring JDBC](https://www.baeldung.com/spring-jdbc-autogenerated-keys)
|
||||
- [Spring Data Annotations](http://www.baeldung.com/spring-data-annotations)
|
||||
- More articles: [[next -->]](/spring-jpa-2)
|
||||
|
||||
|
|
|
@ -1,14 +0,0 @@
|
|||
package com.baeldung.dsrouting;
|
||||
|
||||
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
|
||||
|
||||
/**
|
||||
* Returns thread bound client lookup key for current context.
|
||||
*/
|
||||
public class ClientDataSourceRouter extends AbstractRoutingDataSource {
|
||||
|
||||
@Override
|
||||
protected Object determineCurrentLookupKey() {
|
||||
return ClientDatabaseContextHolder.getClientDatabase();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue