2017-09-14 16 views

回答

1

我不确定容器是否真的保持连接打开。但让我们说,它会。首先,您需要通过CDI注射EntityManager。你可以这样做,像这样:

@ApplicationScoped 
public class EntityManagerProducer { 
    @Produces 
    @PersistenceContext(unitName = "my-pu-name") 
    private EntityManager em; 
} 

那么你的初始化方法中,你可以使用:

public void init() { 
    EntityManager entityManager = CDI.current().select(EntityManager.class).get(); 
    // Do some stuff here 
    CDI.current().select(Entitymanager.class).destroy(entityManager); 
} 

.destroy应确保依存度活跃了。

相关问题