Reputation: 1806
I'm trying to run a project on WAS 9.0.0.1, that currently works fine on WAS 8.5.5.x. The problem seems to be that entitymanager is not being injected into the DAO, but this only happens every other request. So when doing the em.createNamedQuery in the DAO it is throwing a NullPointerException because the em is null, like I explained above the strange thing is that it is only failing like this every other request. Any ideas?
Persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
<persistence-unit name="OperationsAPI" transaction-type="JTA">
<jta-data-source>jdbc/operations</jta-data-source>
<class>com.i3.operations.entities.Operation</class>
<properties>
<property name="openjpa.Log" value="DefaultLevel=WARN, Runtime=INFO, Tool=INFO, SQL=TRACE"/>
</properties>
</persistence-unit>
</persistence>
Resource class:
@Path("/operations")
@Api("Operations API")
@Stateless
public class OperationsService {
public static Logger logger = Logger.getLogger(OperationsService.class.getName());
@Context
UriInfo uriInfo;
@EJB
private static OperationDAO operationDAO;
public OperationsService() {
operationDAO = new OperationDAO();
}
@GET
@ApiOperation(value = "Gets all operations", response = Operation.class, responseContainer = "List")
@Produces(MediaType.APPLICATION_JSON)
public Response getOperationsAll() {
List<Operation> operations = operationDAO.getOperationsAll();
return Response.ok().entity(operations).build();
}
}
DAO:
@Stateless
public class OperationDAO {
public static Logger logger = Logger.getLogger(OperationDAO.class.getName());
@PersistenceContext(unitName="OperationsAPI")
private EntityManager em;
public List<Operation> getOperationsAll() {
logger.info("EntityManager: " + em);
TypedQuery<Operation> query = em.createNamedQuery("Operation.findAll", Operation.class);
return query.getResultList();
}
}
Upvotes: 1
Views: 378
Reputation: 5310
You typically don't want to mix use of both the new operator and also dependency injection (CDI, etc.) to set the same object references in a given class.
If your dependency graph is mostly using dependency injection then dependency injection (DI) should be instantiating all the objects. Otherwise you end up with the case that you create an object instance that DI doesn't know about, so it doesn't know to inject its dependencies into.
This is probably what's happening in your case. Since these EJB instances are pooled, you may end up with some of your instances having been initialized correctly, and some not.
You probably want to change to just (or remove and default):
public OperationsService() {}
I'll mention I'm not sure injecting into a static field is a good idea, but I'm not really enough of an expert to suggest removing the static modifier from operationDAO.
Upvotes: 2