在哪里声明具有多个服务的 JDO Singleton PersistenceManagerFactory
我正在使用 GWT,并且有 4 个需要 PersistenceManagerFactory 的服务实现。我遵循了 Google 关于创建单例类的建议,但是我不确定应该在服务器端代码中的何处实例化和引用该类。
该类看起来像这样
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManagerFactory;
public final class PMF {
private static final PersistenceManagerFactory pmfInstance =
JDOHelper.getPersistenceManagerFactory("transactions-optional");
private PMF() {}
public static PersistenceManagerFactory get() {
return pmfInstance;
}
}
但是在每个服务实现中执行类似的操作似乎最终会根据我收到的错误消息创建单例的唯一副本“应用程序代码试图创建一个名为 transactions-Optional 的 PersistenceManagerFactory,但具有此名称已经存在了!”
@Override
public void addCategory(Category category) {
PersistenceManager pm = PMF.get().getPersistenceManager();
try {
pm.makePersistent(category);
} finally {
pm.close();
}
}
所以基本上第一个调用 addCategory 的 ServiceImpl 没问题,所有其他的都失败并出现上述错误。我在这里遗漏了一些重要的东西,我认为单例类的全部要点是创建一个静态 PersistenceManagerFactory。非常感谢任何对我所缺少的内容的见解。
I am working with GWT and have 4 Service Implementations that need a PersistenceManagerFactory. I followed Google's advice on creating a singleton class, however I am unsure of where this class should be instantiated and referenced from in the server-side code.
The class looks like this
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManagerFactory;
public final class PMF {
private static final PersistenceManagerFactory pmfInstance =
JDOHelper.getPersistenceManagerFactory("transactions-optional");
private PMF() {}
public static PersistenceManagerFactory get() {
return pmfInstance;
}
}
But doing something like this in each Service Implementation seems to end up creating a unique copy of the singleton based on the error message I get "Application code attempted to create a PersistenceManagerFactory named transactions-optional, but one with this name already exists!"
@Override
public void addCategory(Category category) {
PersistenceManager pm = PMF.get().getPersistenceManager();
try {
pm.makePersistent(category);
} finally {
pm.close();
}
}
So basically the first ServiceImpl that calls addCategory is fine, all others fail with the error above. I am missing something vital here, I thought the whole point of the singleton class is to create a static PersistenceManagerFactory. Any insights into what I am missing are greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
PMF 类应该是顶级类,而不是内部类。
通常是
site.server.PMF
。The PMF class should be a top level class, not an internal class.
Usually its
site.server.PMF
.