JDO:PersistenceManager 是单例吗?
只是基础知识:我正在使用由嵌入式 DB4O 数据库支持的 DataNucleus。
如果我做这个简单的测试:
PersistenceManager pm1 = persistenceManagerFactory.getPersistenceManager();
PersistenceManager pm2 = persistenceManagerFactory.getPersistenceManager();
pm1.makePersistent(t1);
pm2.makePersistent(t2);
我得到一个文件锁定异常:
com.db4o.ext.DatabaseFileLockedException: C:\<path>\primary_datastore.data
这告诉我我不知道PersistenceManager
应该如何工作。我以为每当我需要 PersistenceManager 来查询或保存数据时,我只需调用 PersistenceManagerFactory 即可获得线程安全的东西。
- 我需要制作 PersistenceManager 我的整个单身人士 应用?
- 多个怎么办 线程,执行查询和 更新在 JDO/DataNucleus 中有效吗?
Just the basics: I'm using DataNucleus backed with an embedded DB4O database.
If I do this simple test:
PersistenceManager pm1 = persistenceManagerFactory.getPersistenceManager();
PersistenceManager pm2 = persistenceManagerFactory.getPersistenceManager();
pm1.makePersistent(t1);
pm2.makePersistent(t2);
I get a file locked exception:
com.db4o.ext.DatabaseFileLockedException: C:\<path>\primary_datastore.data
Which tells me I don't know how the PersistenceManager
is supposed to work. I thought I just called PersistenceManagerFactory
whenever I needed a PersistenceManager
to query or persist data and I would get something thread safe.
- Do I need to make PersistenceManager
a singleton across my entire
application? - How do multiple
threads, performing queries and
updates work in JDO/DataNucleus?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我是否需要使 PersistenceManager 成为整个应用程序中的单例?
这取决于您的应用程序。如果您开发桌面应用程序,您可能只需要一个持久性管理器。该持久性管理器代表您的桌面应用程序的数据库状态。
但对于其他场景,情况并非如此。例如,在 Web 应用程序中,您希望将请求或会话彼此隔离。因此您使用多个PersistenceManager。例如每个请求一个 PersistenceManager。每个 PersistenceManager 保存当前请求的状态和事务。
因此,PersistenceManager 实例代表一个单元工作/事务。
Do I need to make PersistenceManager a singleton across my entire application?
It depends on you application. If you developing a desktop-application you probably only need only one persistence manager. This persistence-manager represent the state of the database for you desktop-app.
However for other scenarios this isn't the case. For example in web application you want to isolate the requests or sessions from each other. Therefore you use multiple PersistenceManager. For example a PersistenceManager per request. Each PersistenceManager hold the state and transaction for the current request.
So a PersistenceManager-instance represents a unit work / transaction.
因此,按照指南,您的代码应该可以处理此更改:
第二个实例是引用第一个实例化的 PersistenceManager 的代理。
datanucleus.ConnectionFactory<
PersistenceManagerFactory。
例如,以编程方式设置它:
So, following the guide, your code should work with this change:
The second instance is a Proxy referring the first PersistenceManager instantiated.
datanucleus.ConnectionFactory
(or its aliasjavax.jdo.option.Multithreaded
) property at your PersistenceManagerFactory.For instance, setting it programatically:
顺便说一句,我撕掉了 DB4O 并弹出了 NeoDatis(感谢 DN 使这成为一个 5 分钟的任务),六个让我感到困惑和举手投足的测试用例中的每一个都神奇地工作了。并发事务的行为正如我想象的那样,我可以突然持久化可序列化对象的集合(一个单独但同样令人沮丧的问题),以及至少 4 个其他这些对象的派生体。
也许是我错误配置了 DB4O(尽管我的安装是我所能想到的普通安装),但 NeoDatis 在“它确实有效”类别中获得了主要加分。两种普通嵌入式安装,都创建一个文件,都通过 DataNucleus 响应 JDO。
我无法想象在 3 天的地狱般的日子被 5 分钟的 NeoDatis 幸福抹去之后切换回 DB4O。 :)
Incidentally, I ripped out DB4O and popped in NeoDatis (thank you DN for making that a 5 min task) and each of a half dozen test cases that had me baffled and throwing up my hands magically worked. Concurrent transactions behaved as I assumed they should, I could suddenly persist collections of serializable objects (a separate but equally frustrating issue), and at least 4 others that were derivatives of these.
Perhaps my fault for mis-configuring DB4O (though I had as vanilla an install as I could possibly contemplate), but NeoDatis got major bonus points in the "It just works" category. Both vanilla embedded installs, both create a file, both respond to JDO via DataNucleus.
I can't imagine switching back to DB4O after 3 days of hell that were erased with 5 minutes of NeoDatis bliss. :)
当您在“文件”模式下操作时,您究竟希望 db4o 如何支持并发请求?本来以为服务器模式是先决条件
How exactly do you expect db4o to support concurrent requests when you operate in "file" mode ? Would have thought server mode is a prerequisite