在 WebSphere Liberty 中禁用 EntityManager 的缓存/池化

Disable caching / pooling of EntityManager in WebSphere Liberty

我们目前正在将我们的 JEE 应用程序迁移到 WebSphere Liberty。在 WebSphere 8.5 Full Profile 和其他一些应用程序服务器中已经 运行 正常。我们正在使用 Hibernate 中的多租户特性。使用 resolveCurrentTenantIdentifier() 方法解析租户。这在创建 EntityManager 时调用。我们在无状态 Bean 中使用容器管理的 EntityManager。但在 WebSphere 中,无状态 bean 是从池中返回的。如果用户切换租户,则容器 returns 具有相同 EntityManager 的相同无状态 bean(--> 与旧租户)。在 WebSphere 完整配置文件中,EntityManger 由容器重新创建,但在 Liberty 中没有。有谁知道如何避免 EntityManager 的缓存/池化?

我已经尝试自己重新创建 EntityManager,但如果我这样做,我会得到一个应用程序管理的 EntityManger(不是容器管理的),但这不是我想要的。 我还在 Liberty 的 server.xml 中尝试了 jpa 设置 entityManagerPoolCapacity ,这听起来很有希望但没有任何效果:

<jpa entityManagerPoolCapacity="1" />

我们如何创建 EntityManager:

@PersistenceContext(unitName = "PU")
private EntityManager entityManager;

我如何尝试手动重新创建 EntityManager:

EntityManagerFactory entityManagerFactory = entityManager.getEntityManagerFactory();
entityManager = entityManagerFactory.createEntityManager();

JPA: 2.0

休眠:4.2.6

在尝试配置时,EntityManagerPoolCapacity 结果证明是正确的设置。但是您需要将其设置为 0 才能正确禁用 caching/pooling:

<jpa entityManagerPoolCapacity="0" />