protected void cleanupCache() { if (sessionFactory != null) { sessionFactory.getCache().evictCollectionRegions(); sessionFactory.getCache().evictDefaultQueryRegion(); sessionFactory.getCache().evictEntityRegions(); sessionFactory.getCache().evictQueryRegions(); sessionFactory.getCache().evictNaturalIdRegions(); } }
public void evictAll() { sessionFactory.getCache().evictEntityRegions(); // TODO : if we want to allow an optional clearing of all cache data, the additional calls // would be: // sessionFactory.getCache().evictCollectionRegions(); // sessionFactory.getCache().evictQueryRegions(); }
@Override @SuppressWarnings("unchecked") public <T> T unwrap(Class<T> cls) { if (RegionFactory.class.isAssignableFrom(cls)) { return (T) sessionFactory.getSettings().getRegionFactory(); } if (org.hibernate.Cache.class.isAssignableFrom(cls)) { return (T) sessionFactory.getCache(); } throw new PersistenceException("Hibernate cannot unwrap Cache as " + cls.getName()); }
@Test public void testSameIdentifiers() { // create a customer 'steve' in jboss Session session = getNewSession("jboss"); session.beginTransaction(); Customer steve = new Customer(1L, "steve"); session.save(steve); session.getTransaction().commit(); session.close(); // now, create a customer 'john' in acme session = getNewSession("acme"); session.beginTransaction(); Customer john = new Customer(1L, "john"); session.save(john); session.getTransaction().commit(); session.close(); sessionFactory.getStatisticsImplementor().clear(); // make sure we get the correct people back, from cache // first, jboss { session = getNewSession("jboss"); session.beginTransaction(); Customer customer = (Customer) session.load(Customer.class, 1L); Assert.assertEquals("steve", customer.getName()); // also, make sure this came from second level Assert.assertEquals( 1, sessionFactory.getStatisticsImplementor().getSecondLevelCacheHitCount()); session.getTransaction().commit(); session.close(); } sessionFactory.getStatisticsImplementor().clear(); // then, acme { session = getNewSession("acme"); session.beginTransaction(); Customer customer = (Customer) session.load(Customer.class, 1L); Assert.assertEquals("john", customer.getName()); // also, make sure this came from second level Assert.assertEquals( 1, sessionFactory.getStatisticsImplementor().getSecondLevelCacheHitCount()); session.getTransaction().commit(); session.close(); } // make sure the same works from datastore too sessionFactory.getStatisticsImplementor().clear(); sessionFactory.getCache().evictEntityRegions(); // first jboss { session = getNewSession("jboss"); session.beginTransaction(); Customer customer = (Customer) session.load(Customer.class, 1L); Assert.assertEquals("steve", customer.getName()); // also, make sure this came from second level Assert.assertEquals( 0, sessionFactory.getStatisticsImplementor().getSecondLevelCacheHitCount()); session.getTransaction().commit(); session.close(); } sessionFactory.getStatisticsImplementor().clear(); // then, acme { session = getNewSession("acme"); session.beginTransaction(); Customer customer = (Customer) session.load(Customer.class, 1L); Assert.assertEquals("john", customer.getName()); // also, make sure this came from second level Assert.assertEquals( 0, sessionFactory.getStatisticsImplementor().getSecondLevelCacheHitCount()); session.getTransaction().commit(); session.close(); } session = getNewSession("jboss"); session.beginTransaction(); session.delete(steve); session.getTransaction().commit(); session.close(); session = getNewSession("acme"); session.beginTransaction(); session.delete(john); session.getTransaction().commit(); session.close(); }
protected void cleanupCache() { if (sessionFactory != null) { sessionFactory.getCache().evictAllRegions(); } }
public void evict(Class entityClass) { sessionFactory.getCache().evictEntityRegion(entityClass); }
public void evict(Class entityClass, Object identifier) { sessionFactory.getCache().evictEntity(entityClass, (Serializable) identifier); }
public boolean contains(Class entityClass, Object identifier) { return sessionFactory.getCache().containsEntity(entityClass, (Serializable) identifier); }
@Override public Cache getCache() { return delegate.getCache(); }