move datasource routing example to spring-jpa module (#3076)
This commit is contained in:
@@ -1,29 +0,0 @@
|
||||
package org.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");
|
||||
};
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package org.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();
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package org.baeldung.dsrouting;
|
||||
|
||||
public enum ClientDatabase {
|
||||
|
||||
CLIENT_A, CLIENT_B
|
||||
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package org.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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package org.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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user