EntityListener 的@PreUpdate 中的 EJB 上下文 JNDI 查找失败

EJB context JNDI lookup failure within EntityListener's @PreUpdate

我有一个 JPA EntityListener,我在其中为 "java:comp/EJBContext"@PrePersist@PreUpdate 回调中执行相同的 JNDI 查找方法。虽然在 @PrePersist 回调方法期间查找成功,但在 @PreUpdate 回调方法期间一直失败。

我正在使用 Hibernate 4.2。19.Final 和 Websphere AS 8.5.5.3 build cf031430.01 (Java EE 6 / EJB 3.1 / JPA 2.0)。

代码很简单:

public class AuditedEntityListener {

  private static final Logger LOGGER = LoggerFactory.getLogger(AuditedEntityListener.class);

  private static final String STANDARD_EJB_CONTEXT_NAME = "java:comp/EJBContext";

  SessionContext lookupSessionContext() {
    try {
      return InitialContext.doLookup(STANDARD_EJB_CONTEXT_NAME);
    } catch (NamingException ne) {
      LOGGER.error("Lookup failure", ne);
      return null;
    }
  }

  @PrePersist
  void prePersist(IAuditedEntity entity) {
    lookupSessionContext();
  }

  @PreUpdate
  void preUpdate(IAuditedEntity entity) {
    lookupSessionContext();
  }

}

这是痕迹:

javax.naming.NameNotFoundException: Name "comp/EJBContext" not found in context "java:". at com.ibm.ws.naming.ipbase.NameSpace.lookupInternal(NameSpace.java:1229) ~[com.ibm.ws.runtime.jar:na] at com.ibm.ws.naming.ipbase.NameSpace.lookup(NameSpace.java:1142) ~[com.ibm.ws.runtime.jar:na] at com.ibm.ws.naming.urlbase.UrlContextImpl.lookupExt(UrlContextImpl.java:1436) ~[com.ibm.ws.runtime.jar:na] at com.ibm.ws.naming.java.javaURLContextImpl.lookupExt(javaURLContextImpl.java:477) ~[com.ibm.ws.runtime.jar:na] at com.ibm.ws.naming.java.javaURLContextRoot.lookupExt(javaURLContextRoot.java:485) ~[com.ibm.ws.runtime.jar:na] at com.ibm.ws.naming.java.javaURLContextRoot.lookup(javaURLContextRoot.java:370) ~[com.ibm.ws.runtime.jar:na] at org.apache.aries.jndi.DelegateContext.lookup(DelegateContext.java:161) ~[na:na] at javax.naming.InitialContext.lookup(InitialContext.java:422) ~[na:1.7.0] at javax.naming.InitialContext.doLookup(InitialContext.java:295) ~[na:1.7.0] at my.AuditedEntityListener.lookupSessionContext(AuditedEntityListener.java:29) [classes/:na] at my.AuditedEntityListener.getUserName(AuditedEntityListener.java:34) [classes/:na] at my.AuditedEntityListener.preUpdate(AuditedEntityListener.java:74) [classes/:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:94) ~[na:1.7.0] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55) ~[na:1.7.0] at java.lang.reflect.Method.invoke(Method.java:619) ~[na:2.6 (04-09-2014)] at org.hibernate.ejb.event.ListenerCallback.invoke(ListenerCallback.java:48) [hibernate-entitymanager-4.2.19.Final.jar:4.2.19.Final] at org.hibernate.ejb.event.EntityCallbackHandler.callback(EntityCallbackHandler.java:110) [hibernate-entitymanager-4.2.19.Final.jar:4.2.19.Final] at org.hibernate.ejb.event.EntityCallbackHandler.preUpdate(EntityCallbackHandler.java:95) [hibernate-entitymanager-4.2.19.Final.jar:4.2.19.Final] at org.hibernate.ejb.event.EJB3FlushEntityEventListener.invokeInterceptor(EJB3FlushEntityEventListener.java:65) [hibernate-entitymanager-4.2.19.Final.jar:4.2.19.Final] at org.hibernate.event.internal.DefaultFlushEntityEventListener.handleInterception(DefaultFlushEntityEventListener.java:334) [hibernate-core-4.2.19.Final.jar:4.2.19.Final] at org.hibernate.event.internal.DefaultFlushEntityEventListener.scheduleUpdate(DefaultFlushEntityEventListener.java:285) [hibernate-core-4.2.19.Final.jar:4.2.19.Final] at org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:165) [hibernate-core-4.2.19.Final.jar:4.2.19.Final] at org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:230) [hibernate-core-4.2.19.Final.jar:4.2.19.Final] at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:100) [hibernate-core-4.2.19.Final.jar:4.2.19.Final] at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:55) [hibernate-core-4.2.19.Final.jar:4.2.19.Final] at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1195) [hibernate-core-4.2.19.Final.jar:4.2.19.Final] at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:404) [hibernate-core-4.2.19.Final.jar:4.2.19.Final] at org.hibernate.engine.transaction.synchronization.internal.SynchronizationCallbackCoordinatorNonTrackingImpl.beforeCompletion(SynchronizationCallbackCoordinatorNonTrackingImpl.java:109) [hibernate-core-4.2.19.Final.jar:4.2.19.Final] at org.hibernate.engine.transaction.synchronization.internal.RegisteredSynchronization.beforeCompletion(RegisteredSynchronization.java:53) [hibernate-core-4.2.19.Final.jar:4.2.19.Final] at com.ibm.tx.jta.impl.RegisteredSyncs.coreDistributeBefore(RegisteredSyncs.java:291) [com.ibm.tx.jta.jar:na] at com.ibm.ws.tx.jta.RegisteredSyncs.distributeBefore(RegisteredSyncs.java:153) [com.ibm.ws.runtime.jar:na] at com.ibm.ws.tx.jta.TransactionImpl.prePrepare(TransactionImpl.java:2382) [com.ibm.ws.runtime.jar:na] at com.ibm.ws.tx.jta.TransactionImpl.stage1CommitProcessing(TransactionImpl.java:586) [com.ibm.ws.runtime.jar:na] at com.ibm.tx.jta.impl.TransactionImpl.processCommit(TransactionImpl.java:1028) [com.ibm.tx.jta.jar:na] at com.ibm.tx.jta.impl.TransactionImpl.commit(TransactionImpl.java:962) [com.ibm.tx.jta.jar:na] at com.ibm.ws.tx.jta.TranManagerImpl.commit(TranManagerImpl.java:439) [com.ibm.ws.runtime.jar:na] at com.ibm.tx.jta.impl.TranManagerSet.commit(TranManagerSet.java:191) [com.ibm.tx.jta.jar:na] at com.ibm.ejs.csi.TranStrategy.commit(TranStrategy.java:949) [com.ibm.ws.runtime.jar:na] at com.ibm.ejs.csi.TranStrategy.postInvoke(TranStrategy.java:262) [com.ibm.ws.runtime.jar:na] at com.ibm.ejs.csi.TransactionControlImpl.postInvoke(TransactionControlImpl.java:567) [com.ibm.ws.runtime.jar:na] at com.ibm.ejs.container.EJSContainer.postInvoke(EJSContainer.java:4491) [com.ibm.ws.runtime.jar:na]

正如 Brett 指出的那样,这是 IBM 方面的错误。该问题已在 Websphere 8.5.5.9 版中修复。

http://www-01.ibm.com/support/docview.wss?uid=swg1PI52932