8

There are two databases that I have to hit depending on the user request. So I have the entries in persistance.xmlas follows

<persistence-unit name="DB1" transaction-type="JTA">
    <jta-data-source>jdbc/DB1</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
    </properties>
  </persistence-unit>

  <persistence-unit name="DB2" transaction-type="JTA">
    <jta-data-source>jdbc/DB2</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
     <shared-cache-mode>NONE</shared-cache-mode>
    <properties>     
    </properties>
  </persistence-unit>

I have a separate class AppEntityManager that creates the EntityManagerFactory and EntityManager instances

public class AppEntityManager {
    static final EntityManagerFactory emfactory1 = Persistence.createEntityManagerFactory("DB1");

    static final EntityManagerFactory emfactory2 = Persistence.createEntityManagerFactory("DB2");

    public static javax.persistence.EntityManager getEntityManager(String companyId) {            
        switch (companyId) {

            case "0":
                javax.persistence.EntityManager entitymanager = emfactory1.createEntityManager();
                return entitymanager;
            case "1":
                javax.persistence.EntityManager entitymanager1 = emfactory2.createEntityManager();
                return entitymanager1;
        }
        return null;
    }
}

I use the entitymanager as follows

EntityManager e = AppEntityManager.getEntityManager(companyId);
        String SQL = "select v from entityName1 v where v.vendorCode=:vendorCode AND v.status=:status1 AND v.credentialId IN (Select m.credentialId.credentialId from entityName2 m where m.credentialGroupId=:credentialGroupId AND m.status=:status)";

        if(e == null){
            System.out.println("entity manager is null");
        }
        Query query = e.createQuery(SQL);
        if(query==null){
            System.out.println("query is null");
        }else{
            System.out.println("query is not null");
        }
        query.setParameter("vendorCode", vendorCode);
        query.setParameter("status1", "ACTIVE");
        query.setParameter("credentialGroupId", 1);
        query.setParameter("status", "ACTIVE");

        List<entityName1> query1 = query.getResultList();
        System.out.println("query1:" +query1.toString());
        e.close();

The last line that gets printed is

query is not null

Thereafter this line gets printed. I dont know from where but before catch

UnitOfWork(1073609646)--java.lang.NullPointerException

Thereafter the printstack trace is executed in the catch block with the following output. Edit: added complete stack trace

    javax.persistence.PersistenceException: java.lang.NullPointerException
        at org.eclipse.persistence.internal.jpa.QueryImpl.getResultList(QueryImpl.java:480)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
    at com.sun.enterprise.resource.allocator.LocalTxConnectorAllocator.fillInResourceObjects(LocalTxConnectorAllocator.java:117)
    at com.sun.enterprise.resource.pool.ConnectionPool.getResource(ConnectionPool.java:488)
    at com.sun.enterprise.resource.pool.PoolManagerImpl.getResourceFromPool(PoolManagerImpl.java:245)
    at com.sun.enterprise.resource.pool.PoolManagerImpl.getResource(PoolManagerImpl.java:170)
    at com.sun.enterprise.connectors.ConnectionManagerImpl.getResource(ConnectionManagerImpl.java:360)
    at com.sun.enterprise.connectors.ConnectionManagerImpl.internalGetConnection(ConnectionManagerImpl.java:307)
    at com.sun.enterprise.connectors.ConnectionManagerImpl.allocateConnection(ConnectionManagerImpl.java:196)
    at com.sun.enterprise.connectors.ConnectionManagerImpl.allocateConnection(ConnectionManagerImpl.java:171)
    at com.sun.enterprise.connectors.ConnectionManagerImpl.allocateConnection(ConnectionManagerImpl.java:166)
    at com.sun.gjc.spi.base.AbstractDataSource.getConnection(AbstractDataSource.java:114)
    at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:123)
    at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162)
    at org.eclipse.persistence.internal.databaseaccess.DatasourceAccessor.connectInternal(DatasourceAccessor.java:346)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.connectInternal(DatabaseAccessor.java:307)
    at org.eclipse.persistence.internal.databaseaccess.DatasourceAccessor.reconnect(DatasourceAccessor.java:581)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.reconnect(DatabaseAccessor.java:1625)
    at org.eclipse.persistence.internal.databaseaccess.DatasourceAccessor.incrementCallCount(DatasourceAccessor.java:321)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:613)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeCall(DatabaseAccessor.java:558)
    at org.eclipse.persistence.internal.sessions.AbstractSession.basicExecuteCall(AbstractSession.java:2002)
    at org.eclipse.persistence.sessions.server.ServerSession.executeCall(ServerSession.java:570)
    at org.eclipse.persistence.sessions.server.ClientSession.executeCall(ClientSession.java:250)
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:242)
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:228)
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeSelectCall(DatasourceCallQueryMechanism.java:299)
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.selectAllRows(DatasourceCallQueryMechanism.java:694)
    at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllRowsFromTable(ExpressionQueryMechanism.java:2738)
    at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllRows(ExpressionQueryMechanism.java:2691)
    at org.eclipse.persistence.queries.ReadAllQuery.executeObjectLevelReadQuery(ReadAllQuery.java:495)
    at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeDatabaseQuery(ObjectLevelReadQuery.java:1168)
    at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:899)
    at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1127)
    at org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAllQuery.java:403)
    at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(ObjectLevelReadQuery.java:1215)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2896)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1804)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1786)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1751)
    at org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:258)
    at org.eclipse.persistence.internal.jpa.QueryImpl.getResultList(QueryImpl.java:469)
    ... 8 more]]

What do I do to resolve this. Restarting and/or redeploying the application doesn't help. However, deleting the osgi-cache and generated folder in glassfish/domain directory and restarting the server solves the issue temporarily.This issue occurs once a day daily. For some reason I cannot use EJB annotations as well as spring. Where am I going wrong?

Could the following piece of code be the problem?. I get the error only after the thread is created i.e. after it enters this code block

 for (Object vendorThread : vendorDetails) {
            String thread = (String) vendorThread;
            //timeout = details.getTimeout();
            Runnable worker = null;
            try {
                Class c = Class.forName(thread);
                Constructor<?> cons = c.getConstructor(SearchRequest.class, Results.class);
                worker = (Runnable) cons.newInstance(searchRequest, results);
            } catch (Exception e) {
                //e.printStackTrace();
            }
            if (worker == null) {
                System.out.println("------------------------ WORKER IS NULL ---------------");
            }
            executor.execute(worker);
        }
wib
  • 425
  • 3
  • 17
  • Show the full stack, as the internal NPE exception stack will show where it is coming from and what might be null. Things to check though - turn on logging, setting it to finest, and ensure that the EntityManagerFactory isn't being closed somehow - you don't show how you are getting your EntityManager. – Chris Jun 30 '16 at 15:32
  • I use the entitymanager as follows `EntityManager e = AppEntityManager.getEntityManager(companyId);` – wib Jul 01 '16 at 04:13
  • It has nothing to to with `query`. If it was null the stack trace would be different. There is something missing from your connector configuration. – user207421 Jul 01 '16 at 05:12
  • point to note - this occurs only once a day. and it disappears when i delete osgi-cache and generated folder and then restart glassfish – wib Jul 01 '16 at 05:37
  • Do you have the logs that might show what happens leading up to the error? Looks like something with the datasource becomes corrupted - the only solution would be to close all EntityManagerFactory instances and try to reobtain them, that is assuming that the datasource can be reobtained and will work. – Chris Jul 04 '16 at 13:42
  • are you suggesting that I open EntityManagerFactory only when I need to do db operations and close it thereafter? – wib Jul 05 '16 at 06:03

0 Answers0