泽西+ HK2 +灰熊:注入EntityManager的正确方法? [英] Jersey + HK2 + Grizzly: Proper way to inject EntityManager?
问题描述
我现在好奇,最好是将JPA EntityManagers注入到我的资源类中? (我正在考虑一个请求作为一个工作单位)。我目前正在探索的一个选项是以下列方式使用 Factory< EntityManager>
:
class MyEntityManagerFactory实现Factory< EntityManager> {
EntityManagerFactory emf;
public MyEntityManagerFactory(){
emf = Persistence.createEntityManagerFactory(manager1);
}
@Override
public void dispose(EntityManager em){
em.close();
}
@Override
public EntityManager provide(){
return emf.createEntityManager();
}
}
并按如下方式进行绑定: / p>
bindFactory(new MyEntityManagerFactory())
.to(EntityManager.class)
.in(RequestScoped。类);
问题是处理
-method从来没有调用过。
我的问题:
- 注册实体管理员在泽西+ HK2?
- 如果是这样,我应该如何确保我的实体管理器关闭?
(我宁可不依赖重型容器或一个额外的依赖注入库来覆盖这个用例)。
代替 Factory< T> .dispose(T)
,注册注册 CloseableService
可以做你想要的大部分将需要一个可关闭的
适配器。 CloseableService
closing()
退出请求范围后的所有注册资源。
class MyEntityManagerFactory实现Factory< EntityManager> {
private final CloseableService closeableService;
EntityManagerFactory emf;
@Inject
public MyEntityManagerFactory(CloseableService closeableService){
this.closeableService = checkNotNull(closeableService);
emf = Persistence.createEntityManagerFactory(manager1);
}
@Override
public void dispose(EntityManager em){
em.close();
}
@Override
public EntityManager provide(){
final EntityManager em = emf.createEntityManager();
closeableService.add(new Closeable(){
public final void close(){
em.close();
}
});
return em;
}
}
I've managed to set up injection (into resource classes) of my own service classes in Jersey, HK2 and a plain GrizzlyServer. (Basically followed this example.)
I'm now curious what the best is to inject JPA EntityManagers into my resource classes? (I'm currently considering one request as one unit of work). One option that I'm currently exploring is to use a Factory<EntityManager>
in the following way:
class MyEntityManagerFactory implements Factory<EntityManager> {
EntityManagerFactory emf;
public MyEntityManagerFactory() {
emf = Persistence.createEntityManagerFactory("manager1");
}
@Override
public void dispose(EntityManager em) {
em.close();
}
@Override
public EntityManager provide() {
return emf.createEntityManager();
}
}
and bind it as follows:
bindFactory(new MyEntityManagerFactory())
.to(EntityManager.class)
.in(RequestScoped.class);
The problem is that the dispose
-method is never invoked.
My questions:
- Is this the right approach to injecting EntityManagers in Jersey+HK2?
- If so, how should I make sure my EntityManagers are closed properly?
(I'd rather not depend on heavy weight containers or an extra dependency-injection library just to cover this use case.)
In place of Factory<T>.dispose(T)
, registering with the injectable CloseableService
may do most of what you want. A Closeable
adapter will be required. CloseableService
closes()
all registered resources upon exiting the request scope.
class MyEntityManagerFactory implements Factory<EntityManager> {
private final CloseableService closeableService;
EntityManagerFactory emf;
@Inject
public MyEntityManagerFactory(CloseableService closeableService) {
this.closeableService = checkNotNull(closeableService);
emf = Persistence.createEntityManagerFactory("manager1");
}
@Override
public void dispose(EntityManager em) {
em.close();
}
@Override
public EntityManager provide() {
final EntityManager em = emf.createEntityManager();
closeableService.add(new Closeable() {
public final void close() {
em.close();
}
});
return em;
}
}
这篇关于泽西+ HK2 +灰熊:注入EntityManager的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!