如何解决对象管理器已关闭错误?

发布于 2024-11-02 14:44:27 字数 624 浏览 1 评论 0原文

如果有人能给我提供有关如何关闭的教程或最佳实践,我将不胜感激 JDO 连接。 每当我包含finally块时,我都会不断收到javax.jdo.JDOUserException:对象管理器已关闭错误。
我的代码如下:

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;
  }

问候

I'll appreciate if someone can point me to a tutorial or best practice on how to close
JDO connection.
I constantly get javax.jdo.JDOUserException: Object Manager has been closed error whenever I include the finally block.
My code is below:

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;
  }

Regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

如此安好 2024-11-09 14:44:27

避免这种延迟加载问题的一种可能的解决方案是使用 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();
    }        
  }

请参阅此处

One possible solution to avoid this lazy loading problem is to use the size() method forcing the PersistenceManager object to load the result list from datastore before being 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();
        agententity.size()  //Should populate the returned list
        return agententity;
    } finally {
      pm.close();
    }        
  }

Reference here.

老街孤人 2024-11-09 14:44:27

为什么要在这里关闭 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);

Why do you want to close your PersistenceManager here ?
If you want to close the Query you should use either
javax.jdo.Query.closeAll() or javax.jdo.Query.close(Object result).
So you can do either a transient copy of the result and than close the query and its 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();
    }
  }        
} 

or you can close the result later explicitly:

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);
热血少△年 2024-11-09 14:44:27

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

import javax.jdo.FetchPlan;

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

Try setting the fetch plan just after getting the PM, then setting it to all before you perform your query:

import javax.jdo.FetchPlan;

    pm = PMF.get().getPersistenceManager();
    FetchPlan fp = pm.getFetchPlan();
    fp.setGroup(FetchPlan.ALL);
温柔一刀 2024-11-09 14:44:27

实际上,为我解决这个问题的答案在这里:

为什么我是否收到“持久性管理器已关闭”异常

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

Actually, what fixed this for me is answered here:

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

I had an instance reference to the persistence manager, I just made a local instance and all my errors fixed

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文