【问题标题】:How to resolve Object Manager has been closed error?如何解决对象管理器已关闭错误?
【发布时间】:2011-08-07 20:02:57
【问题描述】:

如果有人可以为我提供有关如何关闭的教程或最佳实践,我将不胜感激 JDO 连接。 每当我包含 finally 块时,我都会不断收到 javax.jdo.JDOUserException: Object Manager has been closed 错误。
我的代码如下:

public static List<AgentEntity> findAgentEntityByString(String id) {
    List<AgentEntity> agententity = new ArrayList<AgentEntity>();
    if (id == null) {
      return null;
    }
    try {
        Query q = pm.newQuery("select id from " + AgentEntity.class.getName());
        agententity = (List<AgentEntity>) q.execute();
    } catch(Exception ex) {
        log.warning(ex.getMessage());
    }
        return agententity;
  }

问候

【问题讨论】:

  • 也许可以解决“最终”中发生的情况(我在您的示例中没有看到)。也许事情正在分离?或者您是在 PM 关闭后访问的事情?
  • 如果我包含 finally 块,我得到持久性管理器已关闭错误。所以我直接删除了它。当我这样做时,代码运行良好,但日志仍然抱怨“持久性管理器已关闭”。有了这个,我无法在要显示的 Spring Controller GET 请求中检索存储在我的 ModelAttribute 中的数据。
  • 但是你还没有说在 PM 的“关闭”中发生了什么,或者你的“finally 块”中有什么代码。日志告诉你发生了什么,但我们看不到它
  • 最终在@DataNucleaus,finally { pm.close();}

标签: google-app-engine jdo


【解决方案1】:

避免这种延迟加载问题的一种可能解决方案是使用size() 方法强制PersistenceManager 对象在关闭之前从数据存储加载结果列表。

public static List<AgentEntity> findAgentEntityByString(String id) {
    List<AgentEntity> agententity = new ArrayList<AgentEntity>();
    if (id == null) {
      return null;
    }
    try {
        Query q = pm.newQuery("select id from " + AgentEntity.class.getName());
        agententity = (List<AgentEntity>) q.execute();
        agententity.size()  //Should populate the returned list
        return agententity;
    } finally {
      pm.close();
    }        
  }

参考here

【讨论】:

  • 使用上面的代码,我得到了以下Object Manager has been closed Caused by: javax.jdo.JDOUserException: Object Manager has been closed at org.datanucleus.jdo.NucleusJDOHelper.getJDOExceptionForNucleusException(NucleusJDOHelper.java:375) at org.datanucleus.jdo.JDOPersistenceManager.close(JDOPersistenceManager.java:281) at org.opevel.server.dao.AgentEntityDAOImpl.findAgentEntityByString(AgentEntityDAOImpl.java:95) at org.opevel.web.AgentController.handleRequest(AgentController.java:80) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAcc
【解决方案2】:

你为什么要在这里关闭你的 PersistenceManager ? 如果你想关闭查询,你应该使用 javax.jdo.Query.closeAll()javax.jdo.Query.close(Object result)。 因此,您可以对结果进行临时副本,然后关闭查询及其结果:

public static List<AgentEntity> findAgentEntityByString(String id) {
  if (id == null) {
    return null;
  }
  Query q = null;
  try {
      q = pm.newQuery("select id from " + AgentEntity.class.getName());
      return new ArrayList<AgentEntity>((List<AgentEntity>) q.execute());
  } finally {
    if(q!= null){
        q.closeAll();
    }
  }        
} 

或者您可以稍后显式关闭结果:

public static List<AgentEntity> findAgentEntityByString(String id) {
  if (id == null) {
    return null;
  }
  Query q = pm.newQuery("select id from " + AgentEntity.class.getName());
  return (List<AgentEntity>) q.execute();
  }        
}
....
List agents = X.findAgentEntityByString("Foobar");
....
pm.close(agents);

【讨论】:

  • 我正在从 Spring Controller GET 请求中调用 DAO 方法 findAgentEntityByString(),在这个例子中,这意味着我必须在 Spring 端关闭结果,对吧?
  • 我试过上面的代码,还是不行。下面是错误堆栈:Persistence Manager has been closed Caused by: javax.jdo.JDOFatalUserException: Persistence Manager has been closed at org.datanucleus.jdo.JDOPersistenceManager.assertIsOpen(JDOPersistenceManager.java:2125) at org.datanucleus.jdo.JDOPersistenceManager.newQuery(JDOPersistenceManager.java:1247) at org.datanucleus.jdo.JDOPersistenceManager.newQuery(JDOPersistenceManager.java:1234) at org.opevel.server.dao.AgentEntityDAOImpl.findAgentEntityByString(AgentEntityDAOImpl.java:107) at org.opevel.web.AgentController.handleRequest(
【解决方案3】:

尝试在获取 PM 之后设置获取计划,然后在执行查询之前将其设置为全部:

导入 javax.jdo.FetchPlan;

    pm = PMF.get().getPersistenceManager();
    FetchPlan fp = pm.getFetchPlan();
    fp.setGroup(FetchPlan.ALL);

【讨论】:

    【解决方案4】:

    实际上,在这里为我解决了这个问题:

    Why do I get "Persistence Manager has been closed" exception

    我有一个对持久性管理器的实例引用,我只是创建了一个本地实例并修复了所有错误

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-02
      • 2011-12-03
      • 2020-12-26
      • 2014-03-19
      相关资源
      最近更新 更多