如何使用JDO持久性管理器? [英] How to use JDO persistence manager?

查看:141
本文介绍了如何使用JDO持久性管理器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于如何创建/使用JDO持久性管理器(PM,以下),我有两个问题。

I have two questions regarding how to create / use the JDO persistence manager (PM, hereafter).

在Java Web应用程序中,如果我有10个实体,可以在逻辑上分为2组(例如,5个用户相关实体和5个业务相关实体) )

Say, in a Java web application, if I have 10 entities, which can be logically grouped into 2 groups (for example, 5 user related entities and 5 business related entities)


  1. 我是否需要两个不同的PM来管理这两个组,或者只有一个PM足够?

  2. 关于初始化,我应该使用PM的单例实例(所有用户在给定的时间点使用应用程序共享)还是应该为每个会话创建一个PM?


推荐答案

根据 JDO文档您为每个数据存储区创建一个 PersistenceManagerFactory 。如果您使用JDO通过SQL访问数据库并且您有多个数据库,则每个数据库需要一个 PersistenceManagerFactory (因为您需要指定JDBC URL,用户创建 PersistenceManagerFactory 时的名称和密码。

According to the JDO Documentation you create one PersistenceManagerFactory per datastore. If you are using JDO to access databases via SQL and you have more than one database, then you will need one PersistenceManagerFactory per database (since you need to specify the JDBC URL, user name and password when you create the PersistenceManagerFactory).

对于简单的用例,您只需创建一个 PersistenceManager 何时需要它并在 finally 子句中关闭它(参见持久性管理器文档)。

For simple use cases, you can just create a PersistenceManager when you need it and close it in a finally clause (see the persistence manager documentation).

如果使用事务和更新代码实体可以分布在多个方法或对象上,我建议按需创建 PersistenceManager 并将其存储在 ThreadLocal 中(或者如果使用Guice或Spring,则为请求范围的对象。这将确保执行更新的任何代码都参与当前事务。确保在请求结束时关闭 PersistenceManager

If you use transactions, and the code for updating entities can be spread across multiple methods or objects, I recommend creating the PersistenceManager on demand and storing it in a ThreadLocal (or request-scoped object if you use Guice or Spring). This will make sure any code that does updates participates in the current transaction. Make sure to close the PersistenceManager at the end of the request.

如果您只需要一个持久性管理器工厂,你可以这样做:

If you only need one persistence manager factory, you can do:

public class Datastore {
  private static PersistenceManagerFactory PMF;
  private static final ThreadLocal<PersistenceManager> PER_THREAD_PM
      = new ThreadLocal<PersistenceManager>();

  public static void initialize() {
     if (PMF != null) {
       throw new IllegalStateException("initialize() already called");
     }
     PMF = JDOHelper.getPersistenceManagerFactory("jdo.properties");
  }

  public static PersistenceManager getPersistenceManager() {
    PersistenceManager pm = PER_THREAD_PM.get();
    if (pm == null) {
      pm = PMF.getPersistenceManager();
      PER_THREAD_PM.set(pm);
    }
    return pm;
  }

  public static void finishRequest() {
    PersistenceManager pm = PER_THREAD_PM.get();
    if (pm != null) {
      PER_THREAD_PM.remove();
      Transaction tx = pm.currentTransaction();
      if (tx.isActive()) {
         tx.rollback();
      }
      pm.close();
    }
  }
}

任何需要持久性的代码经理可以调用​​ Datastore.getPersistenceManager()

Any code that needs a persistence manager can call Datastore.getPersistenceManager()

注意:我使用了所有静态方法使其简单用于此目的回答你的问题。如果我使用像Guice这样的依赖注入框架,我会将这些方法设置为非静态,并将 Datastore 绑定为Singleton。

Note: I used all static methods to make it simple for the purposes of answering your question. If I was using a dependency-injection framework like Guice, I would make the methods non-static and bind Datastore as a Singleton.

您可以在Servlet过滤器中调用 finishRequest

You could call finishRequest in a Servlet Filter:

public class PersistenceManagerFilter implements javax.servlet.Filter {

  public init(FilterConfig filterConfig) {
    Datastore.initialize();
  }

  public void doFilter(ServletRequest request, 
      ServletResponse response, FilterChain chain)
      throws IOException, ServletException {
    try {
      chain.doFilter(request, response);
    } finally {
      Datastore.finishRequest();
    }    
  }
}

这篇关于如何使用JDO持久性管理器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆