我有一个这样的应用程序(javaee / wildfly / Maven): 线程processA(在后台触发线程processB)
我将无状态EJB与@PersistenceContext一起使用,在processA中一切正常,我的问题在processB上,持久化在processA内的实体在processB中不是最新的,我认为这是因为@PersistenceContext在每个容器上都有一个缓存工艺对吗? 所以首先我想找到一种方法,当我在processB中使用的唯一方法被调用时“刷新”缓存,但是我什么也没找到……
然后,在更多阅读之后,我发现EntityManager不是线程安全的,最好将@singleton与仅创建一次的EntityManagerFactory一起使用,因为它很昂贵,如下所示: https://subhadipsblog.wordpress.com/2017/09/16/correct-way-of-using-entitymanager-in-singleton-ejb/
@Singleton
public class PersistenceService
{
@PersistenceUnit(name="somePU")
EntityManagerFactory emf;
并按如下所示创建EntityManager:
public void persistenceMethod(Entity myEntity)
{
EntityManager em = emf.createEntityManager();
... //Persistence operations
em.close();
}
编辑: 所以我添加了这个:
@Singleton
public class PersistenceService {
@PersistenceUnit(name="MyPu")
EntityManagerFactory emf;
public ImportState createEntity(MyEntity myEntity) {
EntityManager em = emf.createEntityManager();
em.merge(myEntity);
em.close();
}
public List<MyEntity> getMyEntities() {
EntityManager em = emf.createEntityManager();
List<MyEntity> myEntities = em.createQuery("from MyEntity as myEntity, MyEntity.class)
.getResultList();
em.close();
return myEntities;
}
}
在我的服务中:
@EJB PersistenceService persistenceService;
但是我仍然有同样的问题...
编辑threadB创建:
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(new Runnable() {
@Override
public void run() {
System.out.println("debut tache " + Thread.currentThread().getName());
try {
System.err.println(persistenceService.getMyEntities());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("fin tache");
}
});