/** * Evicts all second-level cache elements which could get stale on entity updates */ protected void evictSecondLevelCache() { Cache cache = entityManager.getEntityManagerFactory().getCache(); // If this DAO is cached, evict the collection regions, as we don't know which ones will point out to it if (hasCache) { synchronized (cache) { // We must invalidate all collection regions, as we don't know which other entities have many-to-many relationships with this one cache.evict(getEntityType()); } } // Evict the query cache region if (queryCacheRegion != null) { synchronized (cache) { cache.evict(getEntityType()); } } }
@Test public void test() { Cache cache = entityManager.getEntityManagerFactory().getCache(); cache.evictAll(); Statistics statistics = ((Session)(entityManager.getDelegate())).getSessionFactory().getStatistics(); statistics.clear(); CommonCourt commonCourt = testPersistenceObjectFactory.createCcCourt(CommonCourtType.APPEAL); commonCourtRepository.findOne(commonCourt.getId()); commonCourtRepository.findOne(commonCourt.getId()); Assert.assertTrue(cache.contains(CommonCourt.class, commonCourt.getId())); Assert.assertTrue(cache.contains(CommonCourtDivision.class, commonCourt.getDivisions().get(0).getId())); Assert.assertTrue(cache.contains(CommonCourtDivision.class, commonCourt.getDivisions().get(1).getId())); cache.evict(CommonCourt.class); cache.evict(CommonCourtDivision.class); Assert.assertFalse(cache.contains(CommonCourt.class, commonCourt.getId())); Assert.assertFalse(cache.contains(CommonCourtDivision.class, commonCourt.getDivisions().get(0).getId())); Assert.assertFalse(cache.contains(CommonCourtDivision.class, commonCourt.getDivisions().get(1).getId())); Assert.assertEquals(5, statistics.getSecondLevelCachePutCount()); // 1 commonCourt + 2 ccDivision + 2 ccDivisionType Assert.assertEquals(2, statistics.getSecondLevelCacheHitCount()); Assert.assertEquals(0, statistics.getSecondLevelCacheMissCount()); }
@Test public void createQueryCacheAndEvictAllThenRetry() throws Exception { List<Author> beforeResults = getAuthorsWithQuery("Author query", "어느나라"); log.warn("#####################################################################"); HibernateEntityManagerFactory entityManagerFactory = (HibernateEntityManagerFactory) EntityTestUtils.getEntityManagerFactory(); org.hibernate.Cache cache = entityManagerFactory.getSessionFactory().getCache(); cache.evictEntityRegions(); cache.evictQueryRegions(); cache.evictDefaultQueryRegion(); cache.evictCollectionRegions(); log.warn("just eviected all."); List<Author> againResults = getAuthorsWithQuery("Author query again after evict all", "어느나라"); assertThat(againResults).isEqualTo(beforeResults); log.warn("#####################################################################"); }
public Cache getCache() { // TODO : cache the cache reference? if ( ! isOpen() ) { throw new IllegalStateException("EntityManagerFactory is closed"); } return new JPACache( sessionFactory ); }
/** * Evicts all second-level cache elements which could get stale on entity updates */ protected void evictSecondLevelCache(E entity) { if (hasCache) { Cache cache = entityManager.getEntityManagerFactory().getCache(); synchronized (cache) { // We must invalidate all collection regions, as we don't know which other entities have many-to-many relationships with this one cache.evict(getEntityType(), entity.getId()); } } }
@Test public void testRoomsAreCached() { Room room = getEntityManager().find(Room.class, WELL_KNOWN_ROOM_ID); assertThat(room, is(not(nullValue()))); Cache cache = getEntityManager().getEntityManagerFactory().getCache(); assertTrue("Rooms should be cached ", cache.contains(Room.class, room.getId())); }
@Test public void testTransientRoomsAreNotCached() { Room room = new Room("", RoomEquipment.BUDGET); Cache cache = getEntityManager().getEntityManagerFactory().getCache(); assertFalse("Rooms should be cached ", cache.contains(Room.class, room.getId())); }
public static void destroy() { if (emf == null) { return; } Cache cache = emf.getCache(); log.debug("###### EVICT ALL ######"); cache.evictAll(); emf.close(); }
private void evict(Cache cache, VARSObject entity) { try { cache.evict(entity.getClass(), entity.getPrimaryKey()); } catch (Exception e) { log.info("Failed to evict " + entity + " from cache", e); } }
@Override public Object invoke(Object... args) { Cache secondLevelCache = getEntityManagerFactory(args).getCache(); if (secondLevelCache != null) { secondLevelCache.evictAll(); } return null; }
@Override public Cache getCache() { return nativeEntityManagerFactory.getCache(); }
@Override public Cache getCache() { return null; }
public Cache getCache() { return delegate.getCache(); }
public Cache getCache() { return null; }
public Cache getCache() { throw new UnsupportedOperationException("Not supported."); }
public Cache getCache() { return CacheDummy.getInstance(); }
public Cache getCache() { return emf.getCache(); }
public Cache getCache() { return entityManagerFactory.getCache(); }
public DAO<T> clearCache() { Cache cache = entityManager.getEntityManagerFactory().getCache(); cache.evict(entityClass); return this; }
public DAO<T> clearAllCache() { Cache cache = entityManager.getEntityManagerFactory().getCache(); cache.evictAll(); return this; }
@Override public Cache getCache() { assert false; return null; }
public Cache getCache() { throw new NoPlansToImplementException(); }
@Override public Cache getCache() { return emf.getCache(); }
@Override public Cache getCache() { return delegate().getCache(); }
/** * @see javax.persistence.EntityManagerFactory#getCache() */ public Cache getCache() { throw new UnsupportedOperationException("JPA is not enabled, this should not be called."); }
public Cache getCache() { return wrapped.getCache(); }
@Test public void testOdataJpaAccessFactory() { ODataJPAFactoryImpl oDataJPAFactoryImpl = new ODataJPAFactoryImpl(); ODataJPAAccessFactory jpaAccessFactory = oDataJPAFactoryImpl .getODataJPAAccessFactory(); ODataJPAContextImpl oDataJPAContextImpl = new ODataJPAContextImpl(); EntityManagerFactory emf = new EntityManagerFactory() { @Override public boolean isOpen() { // TODO Auto-generated method stub return false; } @Override public Map<String, Object> getProperties() { // TODO Auto-generated method stub return null; } @Override public PersistenceUnitUtil getPersistenceUnitUtil() { // TODO Auto-generated method stub return null; } @Override public Metamodel getMetamodel() { // TODO Auto-generated method stub return null; } @Override public CriteriaBuilder getCriteriaBuilder() { // TODO Auto-generated method stub return null; } @Override public Cache getCache() { // TODO Auto-generated method stub return null; } @SuppressWarnings("rawtypes") @Override public EntityManager createEntityManager(final Map arg0) { // TODO Auto-generated method stub return null; } @Override public EntityManager createEntityManager() { // TODO Auto-generated method stub return null; } @Override public void close() { // TODO Auto-generated method stub } }; oDataJPAContextImpl.setEntityManagerFactory(emf); oDataJPAContextImpl.setPersistenceUnitName("pUnit"); assertNotNull(jpaAccessFactory.getODataJPAMessageService(new Locale( "en"))); assertNotNull(jpaAccessFactory.createODataJPAContext()); assertNotNull(jpaAccessFactory .createJPAEdmProvider(oDataJPAContextImpl)); assertNotNull(jpaAccessFactory .createODataProcessor(oDataJPAContextImpl)); }
/** * Clear the second level cache */ public void clear() { Cache cache = kbEmf.getCache(); cache.evictAll(); cache = annoEmf.getCache(); cache.evictAll(); cache = miscEmf.getCache(); cache.evictAll(); }
/** * Setter method for property <tt>cache</tt>. * @param cache value to be assigned to property cache */ public void setCache(Cache cache) { this.cache = cache; }
Cache getCache();