我正在尝试在spring boot(1.4)应用程序中通过架构支持多租户。我的配置中有以下内容:
hibernate:
format_sql: true
default_schema: corrto
multiTenancy: SCHEMA
tenant_identifier_resolver: com.config.HeaderTenantIdentifierResolver
multi_tenant_connection_provider: com.config.SchemaPerTenantConnectionProvider
我的MultiTenantConnectionProvider实现如下:
public class SchemaPerTenantConnectionProvider implements MultiTenantConnectionProvider {
@Autowired
private DataSource dataSource;
@Override
public Connection getAnyConnection() throws SQLException {
return this.dataSource.getConnection();
}
@Override
public void releaseAnyConnection(Connection connection) throws SQLException {
connection.close();
}
@Override
public Connection getConnection(String tenantIdentifier) throws SQLException {
final Connection connection = this.getAnyConnection();
// need to do stuff here
return connection;
}
@Override
public void releaseConnection(String tenantIdentifier, Connection connection) throws SQLException {
}
@Override
public boolean supportsAggressiveRelease() {
return true;
}
@Override
public boolean isUnwrappableAs(Class unwrapType) {
return false;
}
@Override
public <T> T unwrap(Class<T> unwrapType) {
return null;
}
}
失败是因为dataSource
为空。我假设它尚未创建,但我很难通过谷歌找到解决方案。
答案 0 :(得分:0)
我遇到了同样的问题。似乎在yml
,HeaderTenantIdentifierResolver
和SchemaPerTenantConnectionProvider
由hibernate管理。查看here。