我正在尝试使用休眠多租户(来自第 16 章)。
现在我的ExtendedStorageManager使用这个方法来启动一个多租户会话:
public Session getClabSession(int serverId, String customerSchema) {
if (!clabFactories.containsKey(serverId)) {
DbServers d = databaseConfigurations.get(serverId);
clabCustomerConfig.setProperty(
"hibernate.connection.url",
ResourceBundleService.getInstance().decorateBundle(
"database.connection.pattern", d.getHost(),
d.getPort()));
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings(clabCustomerConfig.getProperties())
.buildServiceRegistry();
SessionFactory factory = clabCustomerConfig
.buildSessionFactory(serviceRegistry);
clabFactories.put(d.getId(), factory);
serviceRegistry = null;
factory = null;
}
Session session = clabFactories.get(serverId).withOptions()
.tenantIdentifier(customerSchema).openSession();
session.setDefaultReadOnly(false);
return session;
}
显然,我在测试时遇到了第一个错误,因为我完全错过了这里解释的MultiTenantConnectionProvider的实现。
使用 grepcode 我发现了AbstractMultiTenantConnectionProvider看起来像我想要使用的东西。所以我创建了我的类来扩展它:
public class XMLDMultiTenantConnectionProvider extends
AbstractMultiTenantConnectionProvider {
/**
*
*/
private static final long serialVersionUID = -6679645015449636823L;
@Override
protected ConnectionProvider getAnyConnectionProvider() {
return null;
}
@Override
protected ConnectionProvider selectConnectionProvider(
String tenantIdentifier) {
// TODO Auto-generated method stub
return null;
}
}
但是我现在对在这两种情况下使用什么来获取 ConnectionProvider 有点无能为力。你能帮助我吗?
PS:我找到了ConnectionProviderBuilder。你觉得我可以以此为榜样效仿吗?