【发布时间】:2020-01-29 03:07:44
【问题描述】:
我正在将我的 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);
}
}
我没有收到任何错误,当我进行查询时,它会正确打印 Get connection for tenant 以及与我的其他模式之一的名称匹配的正确 tenantIdentifier。尽管如此,它还是查询test 模式。我错过了什么?谢谢!
编辑:
好像connection.setSchema(tenantIdentifier) 没有效果。在方法的描述中,它说:If the driver does not support schemas, it will silently ignore this request。所以我猜我的驱动程序不支持它?遇到这种情况怎么办?
【问题讨论】:
标签: java spring hibernate multi-tenant