我正在将我的Spring+Hibernate+MySQL配置改为多租户。首先,在我的application.properties
文件中有以下内容:
spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false
spring.datasource.username=root
spring.datasource.password=root
我不确定在引入多租户后是否应该将其连接到特定的模式(
test
)。我觉得这没有意义,因为如果没有提供租户,它应该使用默认模式,否则连接到与租户相关联的模式。然而,如果我删除它,会出现没有提供数据库的错误。其次,多租户部分似乎无法正常工作。我所有的查询都是在
test
模式中进行的。我已经实现了以下MultiTenantConnectionProvider
:@Component
public class TenantConnectionProvider implements MultiTenantConnectionProvider {
private Datasource datasource;
public TenantConnectionProvider(DataSource datasource) {
this.datasource = datasource;
}
...
@Override
public Connection getConnection(String tenantIdentifier) throws SQLException {
logger.info("Get connection for tenant {}", tenantIdentifier);
final Connection connection = getAnyConnection();
connection.setSchema(tenantIdentifier);
return connection;
}
@Override
public void releaseConnection(String tenantIdentifier, Connection connection) throws SQLException {
logger.info("Release connection for tenant {}", tenantIdentifier);
connection.setSchema(DEFAULT_TENANT);
releaseAnyConnection(connection);
}
}
我没有收到任何错误信息,当我进行查询时,它会打印出正确的
tenantIdentifier
和匹配另一个模式名称的正确Get connection for tenant
。但是,它仍然查询test
架构。我错过了什么?谢谢!编辑:
似乎
connection.setSchema(tenantIdentifier)
没有效果。在该方法的描述中,它说:如果驱动程序不支持模式,则会默默忽略此请求。那么我猜我的驱动程序不支持它?这种情况该怎么办?
connection.setSchema(TenantContext.getCurrentTenant());
这段代码是什么意思? - Sajeer Babu