Container无法使用@PersistentContext注入EntityManager

时间:2015-06-16 10:57:24

标签: jpa ejb websphere jpa-2.0 websphere-7

虽然这里已多次询问过这个问题,但我无法找到解决问题的方法。

这是我的EJB:

@Stateless
public class CustomerManagerBean implements CustomerManager,
        CustomerManagerRemote {

@PersistenceContext(unitName = "OldDataBase", type = PersistenceContextType.TRANSACTION)
private EntityManager entityManager;

@TransactionAttribute(value = TransactionAttributeType.SUPPORTS)
    public Customer getCustomer(final CustomerPK customerPK) {

cust = entityManager.find(Customer.class, customerPK);
//Do some stuff
}

* entityManager为null。所以我得到空指针异常。 *

Pesistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
    <persistence-unit name="OldDataBase" transaction-type="JTA">
        <provider>
            org.apache.openjpa.persistence.PersistenceProviderImpl
        </provider>
        <jta-data-source>jdbc/legacyDs</jta-data-source>
        <class>com.rax.pk.CustomerPK</class>
        <class>com.rax.entity.Customer</class>

        <properties>
            <property name="openjpa.TransactionMode" value="managed" />
            <property name="openjpa.ConnectionFactoryMode"
                value="managed" />
            <property name="openjpa.jdbc.DBDictionary" value="db2" />
            <property name="openjpa.DataCache" value="false" />
        </properties>
    </persistence-unit>
    <persistence-unit name="OracleDB" transaction-type="JTA">
        <provider>
            org.apache.openjpa.persistence.PersistenceProviderImpl
        </provider>
        <jta-data-source>jdbc/cssDs</jta-data-source>
        <properties>
            <property name="openjpa.TransactionMode" value="managed" />
            <property name="openjpa.ConnectionFactoryMode"
                value="managed" />
            <property name="openjpa.DataCache" value="false" />
        </properties>
    </persistence-unit>
</persistence>

错误:

0000002a CustAdminProf E com.staples.cacs.appsessions.CustAdminProfileAppSessionBean getCustAdminProfileStatus  Domain layer Error occured while processing  getCustAdminProfileStatus() 
                                 com.staples.cacs.domain.exception.DomainObjectsException: java.lang.NullPointerException
    at com.staples.cacs.domain.entitymanagers.CustomerManagerBean.getCustomer(CustomerManagerBean.java:216)
    at com.staples.cacs.domain.entitymanagers.interfaces.EJSRemote0SLCustomerManagerBean_89163ebf.getCustomer(EJSRemote0SLCustomerManagerBean_89163ebf.java)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:79)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:618)
    at com.ibm.rmi.util.ProxyUtil$4.run(ProxyUtil.java:726)
    at java.security.AccessController.doPrivileged(AccessController.java:241)
    at com.ibm.rmi.util.ProxyUtil.invokeWithClassLoaders(ProxyUtil.java:724)
    at com.ibm.CORBA.iiop.ClientDelegate.invoke(ClientDelegate.java:1161)
    at $Proxy24.getCustomer(Unknown Source)
    at com.staples.cacs.domain.entitymanagers.interfaces._CustomerManagerRemote_Stub.getCustomer(_CustomerManagerRemote_Stub.java)
    at com.staples.cacs.appsessions.CustAdminProfileAppSessionBean.getMasterAccStatus(CustAdminProfileAppSessionBean.java:1791)
    at com.staples.cacs.appsessions.CustAdminProfileAppSessionBean.getCustAdminProfileStatus(CustAdminProfileAppSessionBean.java:823)
    at com.staples.cacs.appsessions.CustAdminProfileAppSessionBean.getCustAdminProfileStatus(CustAdminProfileAppSessionBean.java:760)
    at com.staples.cacs.appsessions.interfaces.EJSRemote0SLCustAdminProfileAppSessionBean_e64c4c7c.getCustAdminProfileStatus(EJSRemote0SLCustAdminProfileAppSessionBean_e64c4c7c.java)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:79)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:618)
    at com.ibm.rmi.util.ProxyUtil$4.run(ProxyUtil.java:726)
    at java.security.AccessController.doPrivileged(AccessController.java:241)
    at com.ibm.rmi.util.ProxyUtil.invokeWithClassLoaders(ProxyUtil.java:724)
    at com.ibm.CORBA.iiop.ClientDelegate.invoke(ClientDelegate.java:1161)
    at $Proxy20.getCustAdminProfileStatus(Unknown Source)
    at com.staples.cacs.appsessions.interfaces._CustAdminProfileAppSessionRemote_Stub.getCustAdminProfileStatus(_CustAdminProfileAppSessionRemote_Stub.java)
    at com.staples.cacs.web.security.PostLoginFilter.doFilter(PostLoginFilter.java:163)
    at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:190)
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:130)
    at com.staples.cacs.web.util.SetHttpHeadersFilter.doFilter(SetHttpHeadersFilter.java:29)
    at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:190)
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:130)
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain._doFilter(WebAppFilterChain.java:87)
    at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:840)
    at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:750)
    at com.ibm.ws.webcontainer.webapp.WebApp.invokeFilters(WebApp.java:3629)
    at com.ibm.ws.wswebcontainer.webapp.WebApp.invokeFilters(WebApp.java:409)
    at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3492)
    at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:269)
    at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:818)
    at com.ibm.ws.wswebcontainer.WebContainer.handleRequest(WebContainer.java:1478)
    at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:126)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:458)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewInformation(HttpInboundLink.java:387)
    at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:102)
    at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
    at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
    at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
    at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:136)
    at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:196)
    at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:751)
    at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:881)
    at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1497)
Caused by: java.lang.NullPointerException
    at com.staples.cacs.domain.entitymanagers.CustomerManagerBean.getCustomer(CustomerManagerBean.java:167)
    ... 52 more

我正在使用RAD 7.5&amp; IBM WebSphere Application Server 6.1。代码似乎没问题。因为这些是我的客户代码,我没有改变。 WAS配置似乎存在一些问题。有谁能够帮我?提前谢谢。

1 个答案:

答案 0 :(得分:0)

日志中应该有其他消息表明这是问题所在,但我知道早期版本的RAD 7.5存在单个persistence.xml文件中定义的多个持久性单元的问题。您是否尝试将persistence.xml修改为仅包含您尝试使用的单个持久性单元?如果不出意外,这可能有助于缩小具体问题的范围。早期版本的OpenJPA在运行时检测到正确的持久性单元时遇到了类似的问题。但是,在任何一种情况下,我都认为记录了消息来指示问题。我尝试这种分离,看看你是否可以取得进步。祝你好运。