Container cannot inject EntityManager using @PersistentContext

468 views Asked by At

Although this question has been asked several times here I cannot find the solution of my problem.

Here is my 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
}

*The entityManager is null. So I am getting null pointer exception. *

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>

Error:

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

I am using RAD 7.5 & IBM WebSphere Application Server 6.1. The code seems to be ok. Because these are my client's code which I have not changed. There seems to be some issues regarding WAS configuration. Can anybody help me? Thanks in advance.

1

There are 1 answers

2
Kevin S On

There should be other messages in the log that would indicate this is the problem, but I know that earlier versions of RAD 7.5 had an issue with multiple persistence-units defined in a single persistence.xml file. Have you tried modifying your persistence.xml to only contain the single persistence-unit that you are trying to utilize? If nothing else, this might help narrow down the specific issue. Earlier versions of OpenJPA had a similar issue at runtime of detecting the proper persistence-unit. But, in either case, I thought there were messages logged to indicate the problem. I'd try this separation to see if you can make progress. Good luck.