Search in sources :

Example 31 with SecondLevelCacheStatistics

use of org.hibernate.stat.SecondLevelCacheStatistics in project ignite by apache.

the class HibernateL2CacheSelfTest method testVersionedEntity.

/**
     * @param accessType Cache access type.
     * @throws Exception If failed.
     */
private void testVersionedEntity(AccessType accessType) throws Exception {
    createSessionFactories(accessType);
    try {
        Session ses = sesFactory1.openSession();
        VersionedEntity e0 = new VersionedEntity(0);
        try {
            Transaction tx = ses.beginTransaction();
            ses.save(e0);
            tx.commit();
        } finally {
            ses.close();
        }
        ses = sesFactory1.openSession();
        long ver;
        try {
            ver = ((VersionedEntity) ses.load(VersionedEntity.class, 0)).getVersion();
        } finally {
            ses.close();
        }
        SecondLevelCacheStatistics stats1 = sesFactory1.getStatistics().getSecondLevelCacheStatistics(VERSIONED_ENTITY_NAME);
        SecondLevelCacheStatistics stats2 = sesFactory2.getStatistics().getSecondLevelCacheStatistics(VERSIONED_ENTITY_NAME);
        assertEquals(1, stats1.getElementCountInMemory());
        assertEquals(1, stats2.getElementCountInMemory());
        ses = sesFactory2.openSession();
        try {
            assertEquals(ver, ((VersionedEntity) ses.load(VersionedEntity.class, 0)).getVersion());
        } finally {
            ses.close();
        }
        assertEquals(1, stats2.getElementCountInMemory());
        assertEquals(1, stats2.getHitCount());
        if (accessType == AccessType.READ_ONLY)
            return;
        e0.setVersion(ver - 1);
        ses = sesFactory1.openSession();
        Transaction tx = ses.beginTransaction();
        try {
            ses.update(e0);
            tx.commit();
            fail("Commit must fail.");
        } catch (StaleStateException e) {
            log.info("Expected exception: " + e);
        } finally {
            tx.rollback();
            ses.close();
        }
        sesFactory1.getStatistics().clear();
        stats1 = sesFactory1.getStatistics().getSecondLevelCacheStatistics(VERSIONED_ENTITY_NAME);
        ses = sesFactory1.openSession();
        try {
            assertEquals(ver, ((VersionedEntity) ses.load(VersionedEntity.class, 0)).getVersion());
        } finally {
            ses.close();
        }
        assertEquals(1, stats1.getElementCountInMemory());
        assertEquals(1, stats1.getHitCount());
        assertEquals(1, stats2.getElementCountInMemory());
        assertEquals(1, stats2.getHitCount());
    } finally {
        cleanup();
    }
}
Also used : Transaction(org.hibernate.Transaction) StaleStateException(org.hibernate.StaleStateException) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Session(org.hibernate.Session)

Example 32 with SecondLevelCacheStatistics

use of org.hibernate.stat.SecondLevelCacheStatistics in project ignite by apache.

the class HibernateL2CacheSelfTest method assertEntityCache.

/**
     * @param entityName Entity name.
     * @param sesFactory Session factory.
     * @param idToName ID to name mapping.
     * @param absentIds Absent entities' IDs.
     */
private void assertEntityCache(String entityName, SessionFactory sesFactory, Map<Integer, String> idToName, Integer... absentIds) {
    assert entityName.equals(ENTITY_NAME) || entityName.equals(ENTITY2_NAME) : entityName;
    sesFactory.getStatistics().clear();
    final Session ses = sesFactory.openSession();
    final boolean entity1 = entityName.equals(ENTITY_NAME);
    try {
        if (entity1) {
            for (Map.Entry<Integer, String> e : idToName.entrySet()) assertEquals(e.getValue(), ((Entity) ses.load(Entity.class, e.getKey())).getName());
        } else {
            for (Map.Entry<Integer, String> e : idToName.entrySet()) assertEquals(e.getValue(), ((Entity2) ses.load(Entity2.class, e.getKey())).getName());
        }
        for (final int id : absentIds) {
            GridTestUtils.assertThrows(log, new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    if (entity1)
                        ((Entity) ses.load(Entity.class, id)).getName();
                    else
                        ((Entity2) ses.load(Entity2.class, id)).getName();
                    return null;
                }
            }, ObjectNotFoundException.class, null);
        }
        SecondLevelCacheStatistics stats = sesFactory.getStatistics().getSecondLevelCacheStatistics(entityName);
        assertEquals(idToName.size(), stats.getHitCount());
        assertEquals(absentIds.length, stats.getMissCount());
    } finally {
        ses.close();
    }
}
Also used : SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) StaleStateException(org.hibernate.StaleStateException) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) Map(java.util.Map) HashMap(java.util.HashMap) Session(org.hibernate.Session)

Aggregations

SecondLevelCacheStatistics (org.hibernate.stat.SecondLevelCacheStatistics)32 Test (org.junit.Test)21 Statistics (org.hibernate.stat.Statistics)18 Session (org.hibernate.Session)11 Map (java.util.Map)9 VersionedItem (org.hibernate.test.cache.infinispan.functional.entities.VersionedItem)9 Item (org.hibernate.test.cache.infinispan.functional.entities.Item)8 OtherItem (org.hibernate.test.cache.infinispan.functional.entities.OtherItem)8 ByRef (org.infinispan.commons.util.ByRef)8 Transaction (org.hibernate.Transaction)6 QueryStatistics (org.hibernate.stat.QueryStatistics)5 EntityManager (javax.persistence.EntityManager)4 TestForIssue (org.hibernate.testing.TestForIssue)4 HashMap (java.util.HashMap)3 VersionedItem (org.hibernate.test.domain.VersionedItem)3 StaleStateException (org.hibernate.StaleStateException)2 CacheEntry (org.hibernate.cache.spi.entry.CacheEntry)2 Contact (org.hibernate.test.cache.infinispan.functional.entities.Contact)2 Item (org.hibernate.test.domain.Item)2 NotificationFilter (com.thoughtworks.go.domain.NotificationFilter)1