1

Hi WAS developers,

I have problem that a JNDI lookup on a declared persistence-context-ref always returns a new EntityManager instance. I expect that within one JTA transaction the container provides me always the same EntityManager instance. But multiple EntityManagers within one transaction causes lock trouble! Furthermore the JPA usage is not optimized as entities might be loaded several times (for each EntityManager) within one transaction.

I have to use persistence-context-ref together with JNDI lookups as I have some EJB2.1 in place within a EJB3.1 module. Furthermore I want to have the EntityManager container-managed.

To reproduce just declare a persistence-context-ref on a EJB2.1 SessionBean:

<persistence-context-ref>
  <persistence-context-ref-name>persistence/MyPersistence</persistence-context-ref-name>
  <persistence-unit-name>MyPersistence</persistence-unit-name>
</persistence-context-ref>

Now make twice a JNDI lookup consecutively within an open JTA transaction:

context.lookup("java:comp/env/persistence/MyPersistence")

You will see that two different EntityManager instances are returned.

Is this a defect in WAS?

4

1 回答 1

1

从 persistence-context-ref 查找返回的 EntityManager 实际上是每个事务 EntityManager 的代理(调试器或打印程序会看到它是 JPATxEntityManager 的实例),因此即使每个查找返回一个唯一的对象,它们都会与相同的底层 EntityManager 交互。

于 2013-04-30T19:37:21.243 回答