我必须根据用户请求点击两个数据库。所以我在Preferences:Build,Execution,Deployment->Build Tools->Maven->Importing
中的条目如下
persistance.xml
我有一个单独的类<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>
,用于创建EntityManagerFactory和EntityManager实例
AppEntityManager
我使用entitymanager如下
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;
}
}
打印的最后一行是
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();
此后打印此行。我不知道从哪里捕获之前
query is not null
此后,在catch块中执行printstack跟踪,输出如下。 编辑:添加完整的堆栈跟踪
UnitOfWork(1073609646)--java.lang.NullPointerException
我该怎么做才能解决这个问题。重新启动和/或重新部署应用程序没有帮助。但是,删除glassfish / domain目录中的osgi-cache和生成的文件夹并重新启动服务器会暂时解决问题。此问题每天发生一次。出于某种原因,我不能使用EJB注释和spring。 我哪里错了?
下面的代码可能是问题吗?我只在创建线程后,即在进入此代码块之后才得到错误
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]]