我在我的应用程序中使用JPA框架。特别是在我点击任何调用JPA操作的链接时,我经常收到IllegalMonitorStateException异常。
错误日志是:
java.lang.IllegalMonitorStateException
at java.util.Vector.isEmpty(Vector.java:500)
at org.eclipse.persistence.internal.jpa.querydef.FromImpl.findJoins(FromImpl.java:567)
at org.eclipse.persistence.internal.jpa.querydef.AbstractQueryImpl.findJoins(AbstractQueryImpl.java:318)
at org.eclipse.persistence.internal.jpa.querydef.CriteriaQueryImpl.translate(CriteriaQueryImpl.java:710)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1412)
at com.abco.revenuecycle.persistence.ResultListProcess.getTypedQuery(ResultListProcess.java:62)
我在catch块中捕获了此Exception
并调用了entityManager.close()
。当我再次单击该链接时,创建新的workunit对象。示例代码在这里:
处理异常:
catch(IllegalMonitorStateException ime){
FactoryHolder factoryHolder = factoryMap.get(schema);
WorkUnit workUnit = factoryHolder.workUnitLocal.get();
if (null != workUnit) {
workUnit.close();
workUnit = null;
}
factoryHolder.workUnitLocal.set(null);
}
创建新对象:
synchronized (lock) {
FactoryHolder factoryHolder = factoryMap.get(schema);
workUnit = factoryHolder.workUnitLocal.get();
//check if entity manager is open
if (null == workUnit || !workUnit.isOpen()) {
return createWorkUnit(schema);
}
}
请提供解决方案以避免此Exception
。
提前致谢。