如何解决对象管理器已关闭错误?
如果有人能给我提供有关如何关闭的教程或最佳实践,我将不胜感激 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
避免这种延迟加载问题的一种可能的解决方案是使用
size()
方法强制PersistenceManager
对象先从数据存储区加载结果列表正在关闭。请参阅此处。
One possible solution to avoid this lazy loading problem is to use the
size()
method forcing thePersistenceManager
object to load the result list from datastore before being closed.Reference here.
为什么要在这里关闭 PersistenceManager ?
如果您想关闭查询,您应该使用
javax.jdo.Query.closeAll()
或javax.jdo.Query.close(Object result)
。因此,您可以执行结果的临时副本,然后关闭查询及其结果:
或者您可以稍后显式关闭结果:
Why do you want to close your PersistenceManager here ?
If you want to close the Query you should use either
javax.jdo.Query.closeAll()
orjavax.jdo.Query.close(Object result)
.So you can do either a transient copy of the result and than close the query and its result:
or you can close the result later explicitly:
尝试在获取 PM 后立即设置获取计划,然后在执行查询之前将其设置为 all:
import javax.jdo.FetchPlan;
Try setting the fetch plan just after getting the PM, then setting it to all before you perform your query:
import javax.jdo.FetchPlan;
实际上,为我解决这个问题的答案在这里:
为什么我是否收到“持久性管理器已关闭”异常
我有一个对持久性管理器的实例引用,我刚刚创建了一个本地实例并修复了所有错误
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