我正在尝试使用 Quarkus 实现多租户,如此处所述。我添加了
quarkus.hibernate-orm.multitenant=DATABASE
至 application.properties
文件并实施 io.quarkus.hibernate.orm.runtime.tenant.TenantResolver
。但是当我尝试使用 EntityManager
时,我得到了这个异常:
org.hibernate.HibernateException: SessionFactory configured for multi-tenancy, but no tenant identifier specified
并且我的
TenantResolver
实现没有被使用。我是不是错过了什么?
创建假的默认数据源,例如:
quarkus.datasource.db-kind = postgresql
quarkus.datasource.username = 1
quarkus.datasource.password = 1
quarkus.datasource.jdbc.url = 1
一个可能的解决方案,但我不确定这是最佳实践。如果有人指出这种方法是否存在问题,我将不胜感激:
在尝试在多线程“ExecutorService”Java Lambda 函数中使用 Quarkus 的多租户时,我遇到了类似的问题。
看来问题不在于Lambda函数的使用,而在于多线程的使用。调试时可以识别出该方法:
io.quarkus.hibernate.orm.runtime.tenant.HibernateCurrentTenantIdentifierResolver.resolveCurrentTenantIdentifier()
从多线程代码调用时无法获取当前活动的请求上下文,因此为tenantId返回空值
我设法通过在线程启动时手动激活上下文来解决这个问题,使用:
Arc.container().requestContext().activate();
这是片段:
ExecutorService threadPool = Executors.newFixedThreadPool(120);
for (int i = 0; i < datasources.length; i++) {
final Integer j = new Integer(i);
threadPool.execute(() -> {
//activates request context
Arc.container().requestContext().activate();
//here goes the rest of the thread code. In some point of it, it is necessary to obtain the tenant identifier
// (...)
});
}