UnitOfWork()--java.lang.NullPointerException in eclipselink
<p>There are two databases that I have to hit depending on the user request. So I have the entries in <code>persistance.xml</code>as follows</p>

<pre><code>&lt;persistence-unit name="DB1" transaction-type="JTA"&gt;
    &lt;jta-data-source&gt;jdbc/DB1&lt;/jta-data-source&gt;
    &lt;exclude-unlisted-classes&gt;false&lt;/exclude-unlisted-classes&gt;
    &lt;properties&gt;
    &lt;/properties&gt;
  &lt;/persistence-unit&gt;

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

<p>I have a separate class <code>AppEntityManager</code> that creates the EntityManagerFactory and EntityManager instances</p>

<pre><code>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;
    }
}
</code></pre>

<p>I use the entitymanager as follows</p>

<pre><code>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&lt;entityName1&gt; query1 = query.getResultList();
        System.out.println("query1:" +query1.toString());
        e.close();
</code></pre>

<p>The last line that gets printed is </p>

<pre><code>query is not null
</code></pre>

<p>Thereafter this line gets printed. I dont know from where but before catch</p>

<pre><code>UnitOfWork(1073609646)--java.lang.NullPointerException
</code></pre>

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

<pre><code>    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]]
</code></pre>

<p>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?</p>

<p>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</p>

<pre><code> for (Object vendorThread : vendorDetails) {
            String thread = (String) vendorThread;
            //timeout = details.getTimeout();
            Runnable worker = null;
            try {
                Class c = Class.forName(thread);
                Constructor&lt;?&gt; 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);
        }
</code></pre>