JDO:PersistenceManager 是单例吗?

发布于 2024-09-29 16:08:33 字数 686 浏览 3 评论 0原文

只是基础知识:我正在使用由嵌入式 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 技术交流群。

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

发布评论

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

评论(4

捎一片雪花 2024-10-06 16:08:33

我是否需要使 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.

哥,最终变帅啦 2024-10-06 16:08:33

因此,按照指南,您的代码应该可以处理此更改:

PersistenceManager pm1 = persistenceManagerFactory.getPersistenceManager();
PersistenceManager pm2 = persistenceManagerFactory.getPersistenceManagerProxy();

pm1.makePersistent(t1);
pm2.makePersistent(t2);

第二个实例是引用第一个实例化的 PersistenceManager 的代理。

例如,以编程方式设置它:

Properties properties = new Properties();
properties.setProperty("javax.jdo.PersistenceManagerFactoryClass",
                "org.datanucleus.jdo.JDOPersistenceManagerFactory");
//configure connection, etc...
properties.setProperty("javax.jdo.option.Multithreaded", "true");
PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(properties);

So, following the guide, your code should work with this change:

PersistenceManager pm1 = persistenceManagerFactory.getPersistenceManager();
PersistenceManager pm2 = persistenceManagerFactory.getPersistenceManagerProxy();

pm1.makePersistent(t1);
pm2.makePersistent(t2);

The second instance is a Proxy referring the first PersistenceManager instantiated.

For instance, setting it programatically:

Properties properties = new Properties();
properties.setProperty("javax.jdo.PersistenceManagerFactoryClass",
                "org.datanucleus.jdo.JDOPersistenceManagerFactory");
//configure connection, etc...
properties.setProperty("javax.jdo.option.Multithreaded", "true");
PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(properties);
早茶月光 2024-10-06 16:08:33

顺便说一句,我撕掉了 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. :)

爱殇璃 2024-10-06 16:08:33

当您在“文件”模式下操作时,您究竟希望 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

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