多租户迁移以使用 quarkus
Multi-tenant migration to work with quarkus
我正在努力迁移应用程序以使用最新版本的 quarkus。该应用程序使用来自 hibernate 接口 的数据库策略实现多租户。 Quarkus 建议 (如果可能) 避免使用 persistence.xml,这种多租户情况的正确方法是什么 quarkus 上下文 ?保留 persistence.xml 或仅使用 quarkus 推荐的 application.properties ?
@Singleton
public class HibernateConnectionProviderFactory implements ConnectionProviderFactory {
private Logger logger = Logger.getLogger(getClass().getName());
private static final String TENANT_ID_PLACEHOLDER = "{tenant.id}";
@Inject
@SystemProperty("multitenant.hibernate.connection.url")
private Optional<String> dbUrl;
@Inject
@SystemProperty("multitenant.hibernate.connection.driver_class")
private Optional<String> dbDriverClass;
@Inject
@SystemProperty("multitenant.hibernate.dialect")
private Optional<String> dbDialect;
@Inject
@SystemProperty("multitenant.hibernate.connection.username")
private Optional<String> dbUsername;
@Inject
@SystemProperty("multitenant.hibernate.connection.password")
private Optional<String> dbPassword;
@Inject
@SystemProperty(value = "multitenant.hibernate.schema.generation", defaultValue = "false")
private boolean generateSchema;
@Inject
@SystemProperty("multitenant.default.tenant.id")
private Optional<String> defaultTenantId;
@Inject
private PersistenceUnitDescriptorFactory persistenceUnitDescriptorFactory;
@Inject
private SchemaGenerator schemaGenerator;
private final ConcurrentHashMap<String, ConnectionProvider> providers = new ConcurrentHashMap<>();
@Override
public Optional<ConnectionProvider> getDefaultConnectionProvider() {
return defaultTenantId.map(this::getConnectionProvider);
}
@Override
public ConnectionProvider getConnectionProvider(String tenantId) {
return providers.computeIfAbsent(tenantId, this::createDriverManagerConnectionProvider);
}
private ConnectionProvider createDriverManagerConnectionProvider(String tenantId) {
final PersistenceUnitDescriptor persistenceUnit = persistenceUnitDescriptorFactory.getPersistenceUnitDescriptor(tenantId);
final Map<Object, Object> persistenceProperties = getPersistenceProperties(tenantId, persistenceUnit.getProperties());
final StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(persistenceProperties).build();
if (generateSchema) {
schemaGenerator.updateSchema(serviceRegistry, persistenceUnit);
}
final ConnectionProvider connectionProvider = serviceRegistry.getService(ConnectionProvider.class);
return connectionProvider;
}
//...
}
目前我们不支持使用 Quarkus application.properties 设置 Hibernate ORM 多租户,因此您应该采用 persistence.xml 方式。
也就是说,如果有足够的牵引力,我们可能会考虑在未来支持它。您能否在 GitHub 上提出问题,以便我们收集有关此功能的反馈?
谢谢!
我正在努力迁移应用程序以使用最新版本的 quarkus。该应用程序使用来自 hibernate 接口 的数据库策略实现多租户。 Quarkus 建议 (如果可能) 避免使用 persistence.xml,这种多租户情况的正确方法是什么 quarkus 上下文 ?保留 persistence.xml 或仅使用 quarkus 推荐的 application.properties ?
@Singleton
public class HibernateConnectionProviderFactory implements ConnectionProviderFactory {
private Logger logger = Logger.getLogger(getClass().getName());
private static final String TENANT_ID_PLACEHOLDER = "{tenant.id}";
@Inject
@SystemProperty("multitenant.hibernate.connection.url")
private Optional<String> dbUrl;
@Inject
@SystemProperty("multitenant.hibernate.connection.driver_class")
private Optional<String> dbDriverClass;
@Inject
@SystemProperty("multitenant.hibernate.dialect")
private Optional<String> dbDialect;
@Inject
@SystemProperty("multitenant.hibernate.connection.username")
private Optional<String> dbUsername;
@Inject
@SystemProperty("multitenant.hibernate.connection.password")
private Optional<String> dbPassword;
@Inject
@SystemProperty(value = "multitenant.hibernate.schema.generation", defaultValue = "false")
private boolean generateSchema;
@Inject
@SystemProperty("multitenant.default.tenant.id")
private Optional<String> defaultTenantId;
@Inject
private PersistenceUnitDescriptorFactory persistenceUnitDescriptorFactory;
@Inject
private SchemaGenerator schemaGenerator;
private final ConcurrentHashMap<String, ConnectionProvider> providers = new ConcurrentHashMap<>();
@Override
public Optional<ConnectionProvider> getDefaultConnectionProvider() {
return defaultTenantId.map(this::getConnectionProvider);
}
@Override
public ConnectionProvider getConnectionProvider(String tenantId) {
return providers.computeIfAbsent(tenantId, this::createDriverManagerConnectionProvider);
}
private ConnectionProvider createDriverManagerConnectionProvider(String tenantId) {
final PersistenceUnitDescriptor persistenceUnit = persistenceUnitDescriptorFactory.getPersistenceUnitDescriptor(tenantId);
final Map<Object, Object> persistenceProperties = getPersistenceProperties(tenantId, persistenceUnit.getProperties());
final StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(persistenceProperties).build();
if (generateSchema) {
schemaGenerator.updateSchema(serviceRegistry, persistenceUnit);
}
final ConnectionProvider connectionProvider = serviceRegistry.getService(ConnectionProvider.class);
return connectionProvider;
}
//...
}
目前我们不支持使用 Quarkus application.properties 设置 Hibernate ORM 多租户,因此您应该采用 persistence.xml 方式。
也就是说,如果有足够的牵引力,我们可能会考虑在未来支持它。您能否在 GitHub 上提出问题,以便我们收集有关此功能的反馈?
谢谢!