我有以下代码。

public class PersistableProcessStepStatus extends ProcessStepStatus {
  @PersistenceContext(unitName = "primary")
  private EntityManager entityManager;
  private void changeFlag(String namedQuery, String flag, boolean enable) {
                getEntityManager().createNamedQuery(namedQuery)
                                .setParameter("a", prozessName)
                                .setParameter("b", vorgangName)
                                .setParameter(flag, enable).executeUpdate();
        }
  [...]
}


我通过JMX控制台调用我的方法,然后将其归结为抛出异常的changeFlag方法。实体管理器在我的应用程序的其他部分中工作。该应用程序在Websphere 8.5.5.1上运行

我没有找到JPATxEntityManager的源代码,所以我真的不知道如何处理。

这是调用createNamedQuery方法引发的异常。

[30/10/14 15:18:34:805 CET] 000002a7 SystemErr     R java.lang.NullPointerException
[30/10/14 15:18:34:805 CET] 000002a7 SystemErr     R    at com.ibm.ws.jpa.management.JPATxEntityManager.getEMInvocationInfo(JPATxEntityManager.java:218)
[30/10/14 15:18:34:805 CET] 000002a7 SystemErr     R    at com.ibm.ws.jpa.management.JPATxEntityManager.getEMInvocationInfo(JPATxEntityManager.java:191)
[30/10/14 15:18:34:806 CET] 000002a7 SystemErr     R    at com.ibm.ws.jpa.management.JPAEntityManager.createNamedQuery(JPAEntityManager.java:312)
[30/10/14 15:18:38:694 CET] 000002a7 SystemErr     R    at public.PersistableProcessStepStatus.changeFlag(PersistableProcessStepStatus.java:34)
[30/10/14 15:18:38:694 CET] 000002a7 SystemErr     R    at public.PersistableProcessStepStatus.enableDebugMode(PersistableProcessStepStatus.java:51)
[30/10/14 15:18:38:694 CET] 000002a7 SystemErr     R    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[30/10/14 15:18:38:694 CET] 000002a7 SystemErr     R    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
[30/10/14 15:18:38:694 CET] 000002a7 SystemErr     R    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
[30/10/14 15:18:38:694 CET] 000002a7 SystemErr     R    at java.lang.reflect.Method.invoke(Method.java:611)
[30/10/14 15:18:38:694 CET] 000002a7 SystemErr     R    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:69)
[30/10/14 15:18:38:694 CET] 000002a7 SystemErr     R    at sun.reflect.GeneratedMethodAccessor33.invoke(Unknown Source)
[30/10/14 15:18:38:695 CET] 000002a7 SystemErr     R    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
[30/10/14 15:18:38:695 CET] 000002a7 SystemErr     R    at java.lang.reflect.Method.invoke(Method.java:611)
[30/10/14 15:18:38:695 CET] 000002a7 SystemErr     R    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:272)
[30/10/14 15:18:38:695 CET] 000002a7 SystemErr     R    at javax.management.modelmbean.RequiredModelMBean$4.run(RequiredModelMBean.java:1152)
[30/10/14 15:18:38:695 CET] 000002a7 SystemErr     R    at java.security.AccessController.doPrivileged(AccessController.java:298)
[30/10/14 15:18:38:695 CET] 000002a7 SystemErr     R    at com.ibm.oti.security.CheckedAccessControlContext.securityCheck(CheckedAccessControlContext.java:30)
[30/10/14 15:18:38:695 CET] 000002a7 SystemErr     R    at sun.misc.JavaSecurityAccessWrapper.doIntersectionPrivilege(JavaSecurityAccessWrapper.java:41)
[30/10/14 15:18:38:695 CET] 000002a7 SystemErr     R    at javax.management.modelmbean.RequiredModelMBean.invokeMethod(RequiredModelMBean.java:1146)
[30/10/14 15:18:38:695 CET] 000002a7 SystemErr     R    at javax.management.modelmbean.RequiredModelMBean.invoke(RequiredModelMBean.java:999)
[30/10/14 15:18:38:696 CET] 000002a7 SystemErr     R    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:847)
[30/10/14 15:18:38:696 CET] 000002a7 SystemErr     R    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:783)
[30/10/14 15:18:38:696 CET] 000002a7 SystemErr     R    at com.ibm.ws.management.AdminServiceImpl$1.run(AdminServiceImpl.java:1335)
[30/10/14 15:18:38:696 CET] 000002a7 SystemErr     R    at com.ibm.ws.security.util.AccessController.doPrivileged(AccessController.java:118)
[30/10/14 15:18:38:696 CET] 000002a7 SystemErr     R    at com.ibm.ws.management.AdminServiceImpl.invoke(AdminServiceImpl.java:1228)
[30/10/14 15:18:38:696 CET] 000002a7 SystemErr     R    at com.ibm.ws.management.remote.AdminServiceForwarder.invoke(AdminServiceForwarder.java:346)
[30/10/14 15:18:38:696 CET] 000002a7 SystemErr     R    at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1466)
[30/10/14 15:18:38:696 CET] 000002a7 SystemErr     R    at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:86)
[30/10/14 15:18:38:696 CET] 000002a7 SystemErr     R    at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1307)
[30/10/14 15:18:38:696 CET] 000002a7 SystemErr     R    at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1399)
[30/10/14 15:18:38:696 CET] 000002a7 SystemErr     R    at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:830)
[30/10/14 15:18:38:697 CET] 000002a7 SystemErr     R    at javax.management.remote.rmi._RMIConnectionImpl_Tie.invoke(_RMIConnectionImpl_Tie.java:751)
[30/10/14 15:18:38:697 CET] 000002a7 SystemErr     R    at javax.management.remote.rmi._RMIConnectionImpl_Tie._invoke(_RMIConnectionImpl_Tie.java:158)
[30/10/14 15:18:38:697 CET] 000002a7 SystemErr     R    at com.ibm.CORBA.iiop.ServerDelegate.dispatchInvokeHandler(ServerDelegate.java:631)
[30/10/14 15:18:38:697 CET] 000002a7 SystemErr     R    at com.ibm.CORBA.iiop.ServerDelegate.dispatch(ServerDelegate.java:501)
[30/10/14 15:18:38:697 CET] 000002a7 SystemErr     R    at com.ibm.rmi.iiop.ORB.process(ORB.java:622)
[30/10/14 15:18:38:697 CET] 000002a7 SystemErr     R    at com.ibm.CORBA.iiop.ORB.process(ORB.java:1581)
[30/10/14 15:18:38:697 CET] 000002a7 SystemErr     R    at com.ibm.rmi.iiop.Connection.doRequestWork(Connection.java:3148)
[30/10/14 15:18:38:697 CET] 000002a7 SystemErr     R    at com.ibm.rmi.iiop.Connection.doWork(Connection.java:3013)
[30/10/14 15:18:38:697 CET] 000002a7 SystemErr     R    at com.ibm.rmi.iiop.WorkUnitImpl.doWork(WorkUnitImpl.java:63)
[30/10/14 15:18:38:697 CET] 000002a7 SystemErr     R    at com.ibm.ejs.oa.pool.PooledThread.run(ThreadPool.java:118)
[30/10/14 15:18:38:698 CET] 000002a7 SystemErr     R    at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1864)

最佳答案

因此,解决方案是:

在persistence.xml中创建persistent-utnit

    <persistence-unit name="second" transaction-type="RESOURCE_LOCAL">


并更改类别:

@TransactionManagement(TransactionManagementType.BEAN)
public class PersistableProcessStepStatus extends ProcessStepStatus {
@PersistenceUnit(unitName = "second")
private EntityManagerFactory entityManagerFactory;

private void changeFlag(String namedQuery, String flag, boolean enable) {
    entityManager = entityManagerFactory.createEntityManager();

    entityManager.getTransaction().begin();
    entityManager.createNamedQuery(namedQuery)
            .setParameter("a", prozessName)
            .setParameter("b", vorgangName)
            .setParameter(flag, enable).executeUpdate();
    entityManager.getTransaction().commit();


}

10-06 02:23