0

I have a web application that I'm attempting to run inside the JBOSS EAP 6 App Server. I am attempting to Inject an EntityManagerFactory into my Singleton Bean.

@Singleton
@Startup
public class RestServerConfigurator {
private static RestServerConfigurator instance;


@PersistenceUnit(unitName = "clinicalTrialsEntityManager")
private EntityManagerFactory emf;

public RestServerConfigurator() {
    if (emf == null) {
        System.out.println("You're null.");
    }
    else {
        System.out.println("Yay! Not null!");
    }
}

To some extent, I know the PersistenceUnit annotation is being evaluated. If I attempt to give it a unitName other than the one mentioned in my persistence.xml, the JBOSS server will complain when I attempt to deploy the application.

Relevant persistence.xml structure.

   <persistence-unit name="clinicalTrialsEntityManager" transaction-type="RESOURCE_LOCAL">
      <jta-data-source>java:jboss/datasources/ClinicalTrialsDS</jta-data-source>

By all accounts, it appears the "emf" variable is never initialized, and that even though it deploys successfully, a null pointer is thrown whenever the EntityManagerFactory is attempted to be used.

I even created a Singleton StartUp Bean to initialize the EntityManagerFactory thinking maybe that's what I was missing and that will somehow tie them together, but to no avail. Please let me know what I'm missing, or if more information is required.

Also, if someone could explain how the EntityManagerFactory is "supposed" to get initialized behind the scenes when the annotation/injection method is used, that would be most appreciated. I'm fairly new to EJB concepts and am having trouble getting my head around it.

10:00:10,348 INFO  [org.jboss.as.jpa] (ServerService Thread Pool -- 135) JBAS011402: Starting Persistence Unit Service 'clinicalTrials.war#clinicalTrialsEntityManager'
10:00:10,348 INFO  [org.hibernate.ejb.Ejb3Configuration] (ServerService Thread Pool -- 135) HHH000204: Processing PersistenceUnitInfo [
    name: clinicalTrialsEntityManager
    ...]
10:00:10,371 INFO  [org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator] (ServerService Thread Pool -- 135) HHH000130: Instantiating explicit connection provider: org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider
10:00:10,411 INFO  [org.hibernate.dialect.Dialect] (ServerService Thread Pool -- 135) HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect

Log output to verify the Bean is initialized:

13:53:36,132 INFO  [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-10) JNDI bindings for session bean named RestServerConfigurator in deployment unit deployment "clinicalTrials.war" are as follows:

java:global/clinicalTrials/RestServerConfigurator!edu.emory.clinical.trials.webapp.server.rest.RestServerConfigurator
java:app/clinicalTrials/RestServerConfigurator!edu.emory.clinical.trials.webapp.server.rest.RestServerConfigurator
java:module/RestServerConfigurator!edu.emory.clinical.trials.webapp.server.rest.RestServerConfigurator
java:global/clinicalTrials/RestServerConfigurator
java:app/clinicalTrials/RestServerConfigurator
java:module/RestServerConfigurator
daniel9x
  • 755
  • 2
  • 11
  • 28
  • Generally you should see persistence unit details in server startup logs if it's initialized properly, did you find it there. – Nayan Wadekar Jan 15 '16 at 12:09
  • Thanks for the comment. In fact, I do see those details which suggest the initialization is working properly. I'll modify the question with the log statements that I'm referring to. – daniel9x Jan 15 '16 at 15:02
  • 1
    How are you invoking your stateless bean, forgot to add before that do you find bean details[JNDI] in logs just to ensure in web application, bean is being initialized. Also can you try creating it - `EntityManagerFactory emf = Persistence.createEntityManagerFactory(UNIT_NAME); `. Also injecting factory in stateless bean is not advisable, inject in singleton & then retrieve entity manager from it. – Nayan Wadekar Jan 15 '16 at 15:45
  • Thanks so much for the feedback. That totally makes sense and I've modified the question (and location of my attempted EMF injection) accordingly. Yes, when I use the Persistence.createEntityManagerFactory(UNIT_NAME) to initialize it works as expected. But trying to use injection, I'm still getting the null. There must be some oversight I'm making. – daniel9x Jan 15 '16 at 19:23
  • you are using transaction-type="RESOURCE_LOCAL" and setting data source in persistence.xml. You don't need data source for resource local type transactions. – Sai prateek Jan 21 '16 at 04:49

2 Answers2

0

All of my issues surrounding dependency injection not functioning properly or as expected (especially in my Rest Service classes) were resolved once I unplugged my Jersey Implementation and relied on JBoss' Tightly coupled RESTEasy module.

To arrive at this conclusion, I used their example apps (specifically helloworld-rs and hibernate4) and reverse engineered from there.

I found an article about extending AbstractBinder and registering that with my application, but it just seemed easier (and required less code) for my purposes to remove Jersey and all the manual "exclude" RESTEasy config that I initially did after reading this article.

Community
  • 1
  • 1
daniel9x
  • 755
  • 2
  • 11
  • 28
0

An entity manager whose transactions are controlled by the application through the EntityTransaction API is a resource-local entity manager. A resource-local entity manager transaction is mapped to a resource transaction over the resource by the persistence provider. Resource-local entity managers may use server or local resources to connect to the database and are unaware of the presence of JTA transactions that may OR may not be active.

<persistence 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"
    version="1.0">

    <persistence-unit name="ShopPU" transaction-type="RESOURCE_LOCAL">
        ...
    </persistence-unit>
</persistence>

Create EntityManager reference using below code

EntityManagerFactory emf = Persistence.createEntityManagerFactory("Order");
        EntityManager em = emf.createEntityManager();

        em.getTransaction().begin();

        ...

        em.getTransaction().commit();

        em.close();
        emf.close ();
Sai prateek
  • 11,842
  • 9
  • 51
  • 66