我有一个应用程序,需要连接到几个不同的模式,但所有类型的所有类型(oracle(。哪个模式来自UI的决定。
如果用户选择schema1,则实体应在schema1中持续存在,如果选择其他,则应在选定的其他架构中。
正在使用Spring Boot Hibernate与" Spring-Boot-Starter-Data-jpa"
我已经创建了下面的数据源类,以便在调用数据层之前,我可以在每次调用数据量对象中更改" schemaname"。
@Component
public class SchemaDatasource extends AbstractDataSource {
private String schemaName;
@Autowired
private DSManager dsm;
public void setSchemaName(String schemaName) {
this.schemaName = schemaName;
}
@Override
public Connection getConnection() throws SQLException {
if (schemaName!= null)
return dsm.getConnection(schemaName);
else
return null;
}
@Override
public Connection getConnection(String username, String password) throws SQLException {
if (schemaName!= null)
return dsm.getConnection(schemaName);
else
return null;
}
}
我的问题是在启动期间," Hibernatejpaautoconfiguration"尝试创建SessionFactory.ding Creation,它试图检查与数据源的连接,但由于ScheManame在启动中无效,因此我的Schemadatasource在我的schemadatasource中,我的SchemadataSource返回了与应用程序Boottrap失败的null连接。/p>
有办法处理此问题。期望与冬眠中的nooptions类似。
在routingDatasource的情况下,我必须设置DefaultDatasource。
Spring boot version: 1.5.9.RELEASE
这是我对数据源的实现
public class DataSourceManager implements DataSource {
private Map<String, DataSource> dataSources = new HashMap<>();
private DataSource dataSource;
public DataSourceManager() {
}
public DataSourceManager(DataSource dataSource) {
this.dataSource = dataSource;
}
public void add(String name, DataSource dataSource) {
dataSources.put(name, dataSource);
}
public void switchDataSource(String name) {
dataSource = dataSources.get(name);
}
@Override
public PrintWriter getLogWriter() throws SQLException {
return dataSource.getLogWriter();
}
@Override
public void setLogWriter(PrintWriter out) throws SQLException {
dataSource.setLogWriter(out);
}
@Override
public void setLoginTimeout(int seconds) throws SQLException {
dataSource.setLoginTimeout(seconds);
}
@Override
public int getLoginTimeout() throws SQLException {
return dataSource.getLoginTimeout();
}
@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
return dataSource.getParentLogger();
}
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
return dataSource.unwrap(iface);
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return dataSource.isWrapperFor(iface);
}
@Override
public Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
@Override
public Connection getConnection(String username, String password) throws SQLException {
return dataSource.getConnection(username, password);
}
}
这是我的配置
@Configuration
public class DataSourceConfig {
@Autowired
private Environment env;
public DataSource makeDataSource(String name) {
return DataSourceBuilder.create()
.driverClassName(env.getProperty("spring.datasource." + name + ".driver-class-name"))
.url(env.getProperty("spring.datasource." + name + ".url")).build();
}
@Bean
public DataSource dataSource() {
DataSourceManager dataSourceManager = new DataSourceManager();
dataSourceManager.add("test1", makeDataSource("test1"));
dataSourceManager.add("test2", makeDataSource("test2"));
dataSourceManager.switchDataSource("test1");
return dataSourceManager;
}
}
这是application.yml
spring:
jpa:
hibernate:
ddl-auto: create
properties:
hibernate:
dialect: org.hibernate.dialect.H2Dialect
datasource:
test1:
name: test2
url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
driver-class-name: org.h2.Driver
username: h2
password: h2
test2:
name: test1
url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
driver-class-name: org.h2.Driver
username: h2
password: h2