Search in sources :

Example 1 with Cache

use of org.hibernate.Cache in project hibernate-orm by hibernate.

the class ReadWriteTest method testEntityCacheContentsAfterEvictAll.

@Test
public void testEntityCacheContentsAfterEvictAll() throws Exception {
    final List<Citizen> citizens = saveSomeCitizens();
    withTxSession(s -> {
        Cache cache = s.getSessionFactory().getCache();
        Statistics stats = sessionFactory().getStatistics();
        SecondLevelCacheStatistics slcStats = stats.getSecondLevelCacheStatistics(Citizen.class.getName());
        assertTrue("2lc entity cache is expected to contain Citizen id = " + citizens.get(0).getId(), cache.containsEntity(Citizen.class, citizens.get(0).getId()));
        assertTrue("2lc entity cache is expected to contain Citizen id = " + citizens.get(1).getId(), cache.containsEntity(Citizen.class, citizens.get(1).getId()));
        assertEquals(2, slcStats.getPutCount());
        cache.evictEntityRegions();
        TIME_SERVICE.advance(1);
        assertEquals(0, slcStats.getElementCountInMemory());
        assertFalse("2lc entity cache is expected to not contain Citizen id = " + citizens.get(0).getId(), cache.containsEntity(Citizen.class, citizens.get(0).getId()));
        assertFalse("2lc entity cache is expected to not contain Citizen id = " + citizens.get(1).getId(), cache.containsEntity(Citizen.class, citizens.get(1).getId()));
        Citizen citizen = s.load(Citizen.class, citizens.get(0).getId());
        assertNotNull(citizen);
        assertNotNull(citizen.getFirstname());
        assertEquals(1, slcStats.getMissCount());
        markRollbackOnly(s);
    });
}
Also used : SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Citizen(org.hibernate.test.cache.infinispan.functional.entities.Citizen) Statistics(org.hibernate.stat.Statistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Cache(org.hibernate.Cache) Test(org.junit.Test)

Example 2 with Cache

use of org.hibernate.Cache in project hibernate-orm by hibernate.

the class ReadWriteTest method testMultipleEvictAll.

@Test
public void testMultipleEvictAll() throws Exception {
    final List<Citizen> citizens = saveSomeCitizens();
    withTxSession(s -> {
        Cache cache = s.getSessionFactory().getCache();
        cache.evictEntityRegions();
        cache.evictEntityRegions();
    });
    withTxSession(s -> {
        Cache cache = s.getSessionFactory().getCache();
        cache.evictEntityRegions();
        s.delete(s.load(Citizen.class, citizens.get(0).getId()));
        s.delete(s.load(Citizen.class, citizens.get(1).getId()));
    });
}
Also used : Citizen(org.hibernate.test.cache.infinispan.functional.entities.Citizen) Cache(org.hibernate.Cache) Test(org.junit.Test)

Example 3 with Cache

use of org.hibernate.Cache in project uPortal by Jasig.

the class PortalRawEventsAggregatorImpl method evictAggregates.

@AggrEventsTransactional
@Override
public void evictAggregates(Map<Class<?>, Collection<Serializable>> entitiesToEvict) {
    int evictedEntities = 0;
    int evictedCollections = 0;
    final Session session = getEntityManager().unwrap(Session.class);
    final SessionFactory sessionFactory = session.getSessionFactory();
    final Cache cache = sessionFactory.getCache();
    for (final Entry<Class<?>, Collection<Serializable>> evictedEntityEntry : entitiesToEvict.entrySet()) {
        final Class<?> entityClass = evictedEntityEntry.getKey();
        final List<String> collectionRoles = getCollectionRoles(sessionFactory, entityClass);
        for (final Serializable id : evictedEntityEntry.getValue()) {
            cache.evictEntity(entityClass, id);
            evictedEntities++;
            for (final String collectionRole : collectionRoles) {
                cache.evictCollection(collectionRole, id);
                evictedCollections++;
            }
        }
    }
    logger.debug("Evicted {} entities and {} collections from hibernate caches", evictedEntities, evictedCollections);
}
Also used : SessionFactory(org.hibernate.SessionFactory) Serializable(java.io.Serializable) Collection(java.util.Collection) EventSession(org.apereo.portal.events.aggr.session.EventSession) Session(org.hibernate.Session) Cache(org.hibernate.Cache)

Example 4 with Cache

use of org.hibernate.Cache in project uPortal by Jasig.

the class PortalRawEventsAggregatorImpl method doAggregateRawEventsInternal.

private EventProcessingResult doAggregateRawEventsInternal() {
    if (!this.clusterLockService.isLockOwner(AGGREGATION_LOCK_NAME)) {
        throw new IllegalStateException("The cluster lock " + AGGREGATION_LOCK_NAME + " must be owned by the current thread and server");
    }
    if (!this.portalEventDimensionPopulator.isCheckedDimensions()) {
        // First time aggregation has happened, run populateDimensions to ensure enough
        // dimension data exists
        final boolean populatedDimensions = this.portalEventAggregationManager.populateDimensions();
        if (!populatedDimensions) {
            this.logger.warn("Aborting raw event aggregation, populateDimensions returned false so the state of date/time dimensions is unknown");
            return null;
        }
    }
    // Flush any dimension creation before aggregation
    final EntityManager entityManager = this.getEntityManager();
    entityManager.flush();
    entityManager.setFlushMode(FlushModeType.COMMIT);
    final IEventAggregatorStatus eventAggregatorStatus = eventAggregationManagementDao.getEventAggregatorStatus(IEventAggregatorStatus.ProcessingType.AGGREGATION, true);
    // Update status with current server name
    final String serverName = this.portalInfoProvider.getUniqueServerName();
    final String previousServerName = eventAggregatorStatus.getServerName();
    if (previousServerName != null && !serverName.equals(previousServerName)) {
        this.logger.debug("Last aggregation run on {} clearing all aggregation caches", previousServerName);
        final Session session = getEntityManager().unwrap(Session.class);
        final Cache cache = session.getSessionFactory().getCache();
        cache.evictEntityRegions();
    }
    eventAggregatorStatus.setServerName(serverName);
    // Calculate date range for aggregation
    DateTime lastAggregated = eventAggregatorStatus.getLastEventDate();
    if (lastAggregated == null) {
        lastAggregated = portalEventDao.getOldestPortalEventTimestamp();
        // No portal events to aggregate, skip aggregation
        if (lastAggregated == null) {
            return new EventProcessingResult(0, null, null, true);
        }
        // First time aggregation has run, initialize the CLEAN_UNCLOSED status to save catch-up
        // time
        final IEventAggregatorStatus cleanUnclosedStatus = eventAggregationManagementDao.getEventAggregatorStatus(IEventAggregatorStatus.ProcessingType.CLEAN_UNCLOSED, true);
        AggregationIntervalInfo oldestMinuteInterval = this.intervalHelper.getIntervalInfo(AggregationInterval.MINUTE, lastAggregated);
        cleanUnclosedStatus.setLastEventDate(oldestMinuteInterval.getStart().minusMinutes(1));
        eventAggregationManagementDao.updateEventAggregatorStatus(cleanUnclosedStatus);
    }
    final DateTime newestEventTime = DateTime.now().minus(this.aggregationDelay).secondOfMinute().roundFloorCopy();
    final Thread currentThread = Thread.currentThread();
    final String currentName = currentThread.getName();
    final MutableInt events = new MutableInt();
    final MutableObject lastEventDate = new MutableObject(newestEventTime);
    boolean complete;
    try {
        currentThread.setName(currentName + "-" + lastAggregated + "_" + newestEventTime);
        logger.debug("Starting aggregation of events between {} (inc) and {} (exc)", lastAggregated, newestEventTime);
        // Do aggregation, capturing the start and end dates
        eventAggregatorStatus.setLastStart(DateTime.now());
        complete = portalEventDao.aggregatePortalEvents(lastAggregated, newestEventTime, this.eventAggregationBatchSize, new AggregateEventsHandler(events, lastEventDate, eventAggregatorStatus));
        eventAggregatorStatus.setLastEventDate((DateTime) lastEventDate.getValue());
        eventAggregatorStatus.setLastEnd(DateTime.now());
    } finally {
        currentThread.setName(currentName);
    }
    // Store the results of the aggregation
    eventAggregationManagementDao.updateEventAggregatorStatus(eventAggregatorStatus);
    complete = complete && (this.eventAggregationBatchSize <= 0 || events.intValue() < this.eventAggregationBatchSize);
    return new EventProcessingResult(events.intValue(), lastAggregated, eventAggregatorStatus.getLastEventDate(), complete);
}
Also used : DateTime(org.joda.time.DateTime) EntityManager(javax.persistence.EntityManager) MutableInt(org.apache.commons.lang.mutable.MutableInt) EventSession(org.apereo.portal.events.aggr.session.EventSession) Session(org.hibernate.Session) Cache(org.hibernate.Cache) MutableObject(org.apache.commons.lang.mutable.MutableObject)

Aggregations

Cache (org.hibernate.Cache)4 EventSession (org.apereo.portal.events.aggr.session.EventSession)2 Session (org.hibernate.Session)2 Citizen (org.hibernate.test.cache.infinispan.functional.entities.Citizen)2 Test (org.junit.Test)2 Serializable (java.io.Serializable)1 Collection (java.util.Collection)1 EntityManager (javax.persistence.EntityManager)1 MutableInt (org.apache.commons.lang.mutable.MutableInt)1 MutableObject (org.apache.commons.lang.mutable.MutableObject)1 SessionFactory (org.hibernate.SessionFactory)1 SecondLevelCacheStatistics (org.hibernate.stat.SecondLevelCacheStatistics)1 Statistics (org.hibernate.stat.Statistics)1 DateTime (org.joda.time.DateTime)1