MultiTenantConnectionProvider实现具有空的自动装配数据源

时间:2016-09-12 20:20:25

标签: spring hibernate spring-boot multi-tenant

我正在尝试在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为空。我假设它尚未创建,但我很难通过谷歌找到解决方案。

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。似乎在ymlHeaderTenantIdentifierResolverSchemaPerTenantConnectionProvider由hibernate管理。查看here