让 JDO 持久性管理器保持活动状态而不是关闭它?
持久化管理器一般需要关闭吗?您能否只保持其中一个打开并一直重复使用它,即重复此模式:
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// do stuff
tx.commit();
} finally {
if (tx.isActive()) tx.rollback();
}
这样做有什么缺点?这似乎是有道理的,因为由于持久性管理器关闭,您永远不需要“分离”对象?
Does a persistence manager generally need to be closed? Can you just keep one open and re-use it all the time, ie just repeat this pattern:
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// do stuff
tx.commit();
} finally {
if (tx.isActive()) tx.rollback();
}
What are the downsides of this? It seems to make sense as you would never need to 'detatch' objects due to the persistence manager being closed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您愿意,可以一直保持打开状态。要考虑的主要问题是当您运行“更新”查询时,您希望更改多快生效。关闭持久性管理器会立即保留这些更改,而不明确这样做将依赖数据存储在方便时保留您的更改。如果您使用事务,则这无关紧要。除此之外,其实没有什么缺点。 PM 第一次初始化(部署后首次使用)时会产生大量 cpu + 时间开销,但之后打开/关闭 PM 基本上是免费的。
You can keep it open all the time if you want. The main issue to consider is when you are running 'update' queries, how quickly do you want the changes to take effect. Closing the persistence manager persists these changes immediately, whereas not doing so explicitly will rely upon the datastore to persist your changes at its own convenience. If you are using transactions, this is irrelevant. Aside from that, there's not really any downside. There's a large cpu + time overhead upon the very first initialization of the PM (first use after you deploy), but after that opening/closing the PM is basically free.