0

I have an EAR app (to deploy on Weblogic 12c), that has a "persist" componenent. The "persist" component uses JPA (implementation: EclipseLink) to persist the objects.

The bean that uses the entityManager is declared as @Stateless and the entity manager is injected though the @PersistenceContext annotation.

The problem is, i have an NullPointerException each time i try to access the entityManager (meaning, he has not been correctly injected).

@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class MyBean implements MyBeanLocal {    

    @PersistenceContext(unitName = "MyPersistenceUnit", type = PersistenceContextType.EXTENDED)
    EntityManager entityManager;

    public void insert(MyObject object) {
        try {
            entityManager.persist(object); //NullPointerException here
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

persistence.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="MyPersistenceUnit"
        transaction-type="JTA">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <jta-data-source>MyDataSource</jta-data-source>
        <class>com.myclasses.MyObject</class>>
    </persistence-unit>

</persistence>

I use a factory to get an instance of MyBean:

public class MyBeanFactory
{
    public static MyBean create()
    {
        return new MyBean();
    }
}

And i use it like this in regular code:

MyBeanLocal bean = MyBeanFactory.create();

MyBeanLocal is the interface for MyBean and it goes like that

@Local(MyBeanLocal.class)
public interface MyBeanLocal {
    public void insert(MyObject object);
}

I tried to inject the MyBean instance through the @EJB annotation, and it fails (NullPointerException whenever i try to use the MyBean supposedly injected instance)

When my application is deployed on the Weblogic server, i don't see neither MyBean, nor MyPersistenceUnit in it (i see the MDBs from other componenents), though i can see MyPersistenceUnit under Deployments > "MyDomain" > Configuration/Settings > Persistence. MyDataSource is okay and connected.

There is the startup log for the weblogic server.

####<15 juil. 2015 09 h 52 CEST> <Notice> <Security> <MyUsername> <AdminServer> <[ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946746252> <BEA-090082> <Security initializing using security realm myrealm.> 
####<15 juil. 2015 09 h 52 CEST> <Notice> <WebLogicServer> <MyUsername> <AdminServer> <[STANDBY] ExecuteThread: '3' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946771648> <BEA-000365> <Server state changed to STANDBY.> 
####<15 juil. 2015 09 h 52 CEST> <Notice> <WebLogicServer> <MyUsername> <AdminServer> <[STANDBY] ExecuteThread: '3' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946771650> <BEA-000365> <Server state changed to STARTING.> 
####<15 juil. 2015 09 h 53 CEST> <Warning> <EJB> <MyUsername> <AdminServer> <[ACTIVE] ExecuteThread: '3' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946795598> <BEA-012035> <The Remote interface method: public abstract boolean com.myclasses.UtilityRemote.send(com.mytypes.ReqType,java.lang.String) in EJB UtilityBean contains a parameter of type com.mytypes.ReqType which is not serializable. Though the EJB IMMJMSUtilityBean has call-by-reference set to false, this parameter is not serializable and hence will be passed by reference. A parameter can be passed using call-by-value only if the parameter type is serializable.> 
####<15 juil. 2015 09 h 53 CEST> <Notice> <Log Management> <MyUsername> <AdminServer> <[ACTIVE] ExecuteThread: '3' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946808540> <BEA-170027> <The server has successfully established a connection with the Domain level Diagnostic Service.> 
####<15 juil. 2015 09 h 53 CEST> <Notice> <WebLogicServer> <MyUsername> <AdminServer> <[STANDBY] ExecuteThread: '1' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946809347> <BEA-000365> <Server state changed to ADMIN.> 
####<15 juil. 2015 09 h 53 CEST> <Notice> <WebLogicServer> <MyUsername> <AdminServer> <[STANDBY] ExecuteThread: '1' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946809503> <BEA-000365> <Server state changed to RESUMING.> 
####<15 juil. 2015 09 h 53 CEST> <Notice> <Server> <MyUsername> <AdminServer> <[ACTIVE] ExecuteThread: '3' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946809774> <BEA-002613> <Channel "Default[1]" is now listening on 127.0.0.1:7001 for protocols iiop, t3, ldap, snmp, http.> 
####<15 juil. 2015 09 h 53 CEST> <Warning> <Server> <MyUsername> <AdminServer> <DynamicListenThread[Default]> <<WLS Kernel>> <> <> <1436946809774> <BEA-002611> <The hostname "MyUsername.MyProxy.com", maps to multiple IP addresses: 172.23.247.5, 0:0:0:0:0:0:0:1.> 
####<15 juil. 2015 09 h 53 CEST> <Notice> <Server> <MyUsername> <AdminServer> <[ACTIVE] ExecuteThread: '3' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946809775> <BEA-002613> <Channel "Default[2]" is now listening on 0:0:0:0:0:0:0:1:7001 for protocols iiop, t3, ldap, snmp, http.> 
####<15 juil. 2015 09 h 53 CEST> <Notice> <Server> <MyUsername> <AdminServer> <[ACTIVE] ExecuteThread: '3' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946809775> <BEA-002613> <Channel "Default" is now listening on 172.23.247.5:7001 for protocols iiop, t3, ldap, snmp, http.> 
####<15 juil. 2015 09 h 53 CEST> <Notice> <WebLogicServer> <MyUsername> <AdminServer> <[ACTIVE] ExecuteThread: '3' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946809776> <BEA-000331> <Started the WebLogic Server Administration Server "AdminServer" for domain "test" running in development mode.> 
####<15 juil. 2015 09 h 53 CEST> <Notice> <WebLogicServer> <MyUsername> <AdminServer> <[ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946810276> <BEA-000360> <The server started in RUNNING mode.> 
####<15 juil. 2015 09 h 53 CEST> <Notice> <WebLogicServer> <MyUsername> <AdminServer> <[ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)'> <<WLS Kernel>> <> <> <1436946810280> <BEA-000365> <Server state changed to RUNNING.> 

I suspect that the problem is that this bean is not correctly processed by the weblogic server, but i have no idea how to fix it.

If anyone could help me, i'd be thankful. Ask me for more code/config/context if you need to.

clapsus
  • 442
  • 6
  • 19
  • Can you post the code you use to get the MyBean instance? The start up log may be helpful too, if we can fit it in, since it sounds like weblogic is failing to deploy the bean. – hugh Jul 11 '15 at 12:43
  • i edited the original message with the info you requested, nothing seems wrong in the startup log – clapsus Jul 15 '15 at 08:09

1 Answers1

1

The problem here is how you're getting hold of the EJB reference - if you construct the instance yourself using "new", what you'll get is just a POJO rather than an EJB - this means you won't have access to any EJB services like dependency injection and transaction management.

To get hold of a genuine EJB, you should look up your bean in the initial context:

 MyBeanRemote bean = (MyBeanRemote) new InitialContext().lookup("MyBean/remote");
hugh
  • 2,237
  • 1
  • 12
  • 25
  • 1
    Either will work (I've just used Remote because that's what's in the link and I'm being lazy). Just check you use the right JNDI lookup and cast for the type you want. – hugh Jul 15 '15 at 12:50
  • Sorry i deleted my comment ("Does it work with local or does is have to be remote") by misclicking. The problem is that MyBean doesn't seem to be deployed on the weblogic server, and thus, i can't neither see it in the JNDI tree and nor look it up using the code you gave me. The thing is that the stateless beans that are declared in another component of the EAR (the 'EJB' component) are properly deployed, but not those in the 'PERSIST' component even though while working on Eclipse, i can see that it does recognize the stateless beans ([screenshot](http://i.imgur.com/FPEU1za.png)). – clapsus Jul 15 '15 at 13:02
  • Sorry for the double comment, actually i solved the problem by adding the "PERSIST" componenet as an EJB module, and using "java:global.EARNAME.COMPONENETNAME.MyBean!com.myclasses.MyBeanLocal" but i still have an `EJB Exception` as `Extended Persistence Contexts can only be invoked from within the context of the stateful session bean that declares the Extended Persistence Context." at weblogic.persistence.ExtendedEntityManagerProxyImpl.getPersistenceContext(ExtendedEntityManagerProxyImpl.java:62)` – clapsus Jul 15 '15 at 14:51
  • Fantastic! Do you want to raise that as a separate question? I'm pretty rusty on extended persistence contexts, but it sounds interesting! – hugh Jul 15 '15 at 16:21
  • 1
    The problem with that exception is that i was using the extended persistence context, while using a stateless EJB. I used transaction-scoped persistence context instead, and it worked just fine. No need for another question. Thanks for your help Hugh! – clapsus Jul 16 '15 at 10:56