@PersistenceContext(unitName="pu") private EntityManager em; public void persist(Entity entity) { em.persist(entity); em.flush(); em.getEntityManagerFactory().getCache().evictAll(); }
Hi,
In which way do you check is data stale?
em.getEntityManagerFactory().getCache().evictAll(); operates on the shared second level cache, but the EntityManager itself still has a cache of managed entities. You can simply call em.clear() after em.flush() to detach all managed entities.
em.flush(); em.clear(); em.getEntityManagerFactory().getCache().evictAll();