2

I've been working on a REST project on Netbeans 8.2 (compiled using Java 1.7). I'm deploying my web project on a Weblogic Server 12.1.2 and using JPA (w/ Eclipselink) as the persistence engine referencing a JTA datasource that is configured on server.

The problem I have is common from what I investigated in google; however, I was not able to find any solution on the Web that helps me with the issue.

Basically, this is how my persistence.xml file is defined:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
  <persistence-unit name="gchPermissionPU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc.vz.customer.ds</jta-data-source>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
      <property name="eclipselink.target-server" value="WebLogic"/>
      <property name="javax.persistence.query.timeout" value="660000"/>
      <property name="eclipselink.logging.level" value="ALL"/>
      <property name="eclipselink.persistence-context.flush-mode" value="COMMIT"/>
      <property name="eclipselink.cache.shared.default" value="false"/>
    </properties>
  </persistence-unit>
</persistence>

This is how I use the EntityManager on a class:

@PersistenceContext(unitName = "gchPermissionPU")
protected EntityManager entityManager;

I get the NullPointerException whenever this line is called (the only one in which I reference entityManager by now):

Query permissionsQuery = entityManager.createNativeQuery(nativeSqlQuery.toString());

I believe the NullPointer comes up because the entityManager variable is null.

Here's the basic data source configuration on my weblogic:

enter image description here

enter image description here

I wonder if I need some kind of additional configuration in the weblogic side or any missing piece of code.

Thanks in advance for your time and help. Any clue/feedback is appreciated.

Community
  • 1
  • 1
Marcelo Tataje
  • 3,849
  • 1
  • 26
  • 51

1 Answers1

3

I managed to solve my problem by manually creating EntityManagerFactory and EntityManager, from this answer:

PersistenceContext EntityManager injection NullPointerException

Seems like without EJB references, you need to force the creation of your persistence components. So my team came up with the following solution:

//1. Define the components required for persistence:

@PersistenceUnit(unitName = "gchPermissionPU")
private EntityManagerFactory entityManagerFactory;
private EntityManager entityManager;

//2. Just a method that will create entity manager factory if it is "null"

public final EntityManager getEntityManager() {
    if (entityManagerFactory == null) {
        entityManagerFactory = Persistence.createEntityManagerFactory("gchPermissionPU");
    }
    if (entityManager == null) {
        entityManager = entityManagerFactory.createEntityManager();
    }

    return entityManager;
}

However, I wonder to know if this is the best solution for a Web application that will be used by > 1000 users. I can't believe that a Web application should rely on EJB for injection (maybe because it is Weblogic) but I remember I implemented a similar application on Tomcat some time ago and didn't need to make this "workaround".

If somebody has additional comments or feedback, please let me know.

Thanks.

Community
  • 1
  • 1
Marcelo Tataje
  • 3,849
  • 1
  • 26
  • 51