如何在 jersey / hk2 应用程序中正确配置 EntityManager?

How do I properly configure an EntityManager in a jersey / hk2 application?

我有一个使用 JPA 持久性的 jersey-2 / hk2 应用程序。 EntityManager 像这样在启动时绑定

public MyApplication() {
    // ...
    register(new AbstractBinder() {
        @Override
        public void configure() {
          bindFactory(EmFactory.class)
            .to(EntityManager.class)
            .in(RequestScoped.class);
        }
    });
}

工厂 class 为

public class EmFactory implements Factory<EntityManager> {

    private static final String PERSISTENCE_UNIT = "unit";

    private EntityManagerFactory emf;
    private CloseableService closeableService;

    @Inject
    public EmFactory(@Named(PERSISTENCE_UNIT) String persistenceUnit,
            CloseableService closeableService) {
        emf = Persistence.createEntityManagerFactory(persistenceUnit);
        this.closeableService = closeableService;
    }

    @Override
    public EntityManager provide() {
        final EntityManager entityManager = emf.createEntityManager();
        closeableService.add(new Closeable() {

            @Override
            public void close() throws IOException {
                if(entityManager.isOpen()) {
                    entityManager.close();
                }
            }
        });
        return entityManager;
    }

    @Override
    public void dispose(EntityManager entityManager) {
        if(entityManager.isOpen()) {
            entityManager.close();
        }
    }
}

这是可行的,但是对于每个请求,我都会在日志中收到一条关于 EntityManager 已经注册的警告:

HHH000436: Entity manager factory name (unit) is already registered. \
  If entity manager will be clustered or passivated, specify a unique \
  value for property 'hibernate.ejb.entitymanager_factory_name'

我做错了什么?在 jersey-2 / hk2 应用程序中初始化 EntityManager 的正确方法是什么?

一个选择是不在 EMFactory(在请求范围内)中创建新的 EntityManagerFactory,而是为 EntityManagerFactory 创建一个单例工厂,然后只需将 EntityManagerFactory 注入 EMFactory.

public class EMFFactory implements Factory<EntityManagerFactory> {
    private final EntityManagerFactory emf;
    public EMFFactory (){
        emf = Persistence.createEntityManagerFactory(persistenceUnit);
    }
    public EntityManagerFactory provide() {
        return emf;
    }
    ...
}

public class EMFactory implements Factory<EntityManager> {
    private final EntityManager em;

    @Inject
    public EMFactory (EntityManagerFactory emf){
        em = emf.createEntityManager();
    }
    public EntityManager provide() {
        return em;
    }
    ...
}

还没有测试这个确切的实现,但它应该看起来像这样。我以前用过这个模式。

register(new AbstractBinder() {
    @Override
    public void configure() {
      bindFactory(EMFFactory.class).to(EntityManagerFactory.class).in(Singleton.class);
      bindFactory(EMFactory.class).to(EntityManager.class).in(RequestScoped.class);
    }
});

更新

关于上面的例子需要注意的一点是它不会清理资源,即 EntityManager 应该关闭;它不会自行关闭。 Factory class 中有一个 dispose 方法需要重写,但根据我的经验,Jersey 从未调用过该方法。

我们可以做的是将 EntityManager 添加到 [CloseableService][1]

public class EMFactory implements Factory<EntityManager> {
    private final EntityManagerFactory emf;
    private final CloseableService closeService;

    @Inject
    public EMFactory (EntityManagerFactory emf, CloseableService closeService){
        this.emf = emf;
        this.closeService = closeService;
    }
    public EntityManager provide() {
        final EntityManager em = emf.createEntityManager();
        this.closeService.add(new Closeable(){
            @Override
            public void close() {
                em.close();
            }
        });
        return em;
    }
    ...
}

这样可以确保 EntityManager 在请求结束时关闭。