Search in sources :

Example 1 with SecondLevelCacheStatistics

use of org.hibernate.stat.SecondLevelCacheStatistics in project hibernate-orm by hibernate.

the class EhCacheTest method testQueryCacheInvalidation.

@Test
public void testQueryCacheInvalidation() {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Item i = new Item();
    i.setName("widget");
    i.setDescription("A really top-quality, full-featured widget.");
    s.persist(i);
    t.commit();
    s.close();
    SecondLevelCacheStatistics slcs = s.getSessionFactory().getStatistics().getSecondLevelCacheStatistics(Item.class.getName());
    assertEquals(slcs.getPutCount(), 1);
    assertEquals(slcs.getElementCountInMemory(), 1);
    assertEquals(slcs.getEntries().size(), 1);
    s = openSession();
    t = s.beginTransaction();
    i = (Item) s.get(Item.class, i.getId());
    assertEquals(slcs.getHitCount(), 1);
    assertEquals(slcs.getMissCount(), 0);
    i.setDescription("A bog standard item");
    t.commit();
    s.close();
    assertEquals(slcs.getPutCount(), 2);
    Object entry = slcs.getEntries().get(i.getId());
    Map map;
    map = getMapFromCacheEntry(entry);
    assertTrue(map.get("description").equals("A bog standard item"));
    assertTrue(map.get("name").equals("widget"));
    // cleanup
    s = openSession();
    t = s.beginTransaction();
    s.delete(i);
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Map(java.util.Map) Session(org.hibernate.Session) Test(org.junit.Test)

Example 2 with SecondLevelCacheStatistics

use of org.hibernate.stat.SecondLevelCacheStatistics in project hibernate-orm by hibernate.

the class EhCacheTest method testStaleWritesLeaveCacheConsistent.

@SuppressWarnings({ "UnnecessaryBoxing", "UnnecessaryUnboxing", "UnusedAssignment" })
@Test
public void testStaleWritesLeaveCacheConsistent() {
    Session s = openSession();
    Transaction txn = s.beginTransaction();
    VersionedItem item = new VersionedItem();
    item.setName("steve");
    item.setDescription("steve's item");
    s.save(item);
    txn.commit();
    s.close();
    Long initialVersion = item.getVersion();
    // manually revert the version property
    item.setVersion(Long.valueOf(item.getVersion().longValue() - 1));
    try {
        s = openSession();
        txn = s.beginTransaction();
        s.update(item);
        txn.commit();
        s.close();
        fail("expected stale write to fail");
    } catch (Throwable expected) {
        // expected behavior here
        if (txn != null) {
            try {
                txn.rollback();
            } catch (Throwable ignore) {
            }
        }
    } finally {
        if (s != null && s.isOpen()) {
            try {
                s.close();
            } catch (Throwable ignore) {
            }
        }
    }
    // check the version value in the cache...
    SecondLevelCacheStatistics slcs = sessionFactory().getStatistics().getSecondLevelCacheStatistics(VersionedItem.class.getName());
    Object entry = slcs.getEntries().get(item.getId());
    Long cachedVersionValue;
    //		} else
    if (entry.getClass().getName().equals("org.hibernate.cache.ehcache.internal.strategy.AbstractReadWriteEhcacheAccessStrategy$Lock")) {
    //FIXME don't know what to test here
    } else {
        cachedVersionValue = (Long) getMapFromCacheEntry(entry).get("_version");
        assertEquals(initialVersion.longValue(), cachedVersionValue.longValue());
    }
    // cleanup
    s = openSession();
    txn = s.beginTransaction();
    item = (VersionedItem) s.load(VersionedItem.class, item.getId());
    s.delete(item);
    txn.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Session(org.hibernate.Session) Test(org.junit.Test)

Example 3 with SecondLevelCacheStatistics

use of org.hibernate.stat.SecondLevelCacheStatistics in project hibernate-orm by hibernate.

the class ConcurrentWriteTest method testSingleUser.

@Test
public void testSingleUser() throws Exception {
    // setup
    sessionFactory().getStatistics().clear();
    // wait a while to make sure that timestamp comparison works after invalidateRegion
    TIME_SERVICE.advance(1);
    Customer customer = createCustomer(0);
    final Integer customerId = customer.getId();
    getCustomerIDs().add(customerId);
    // wait a while to make sure that timestamp comparison works after collection remove (during insert)
    TIME_SERVICE.advance(1);
    assertNull("contact exists despite not being added", getFirstContact(customerId));
    // check that cache was hit
    SecondLevelCacheStatistics customerSlcs = sessionFactory().getStatistics().getSecondLevelCacheStatistics(Customer.class.getName());
    assertEquals(1, customerSlcs.getPutCount());
    assertEquals(1, customerSlcs.getElementCountInMemory());
    assertEquals(1, customerSlcs.getEntries().size());
    log.infof("Add contact to customer {0}", customerId);
    SecondLevelCacheStatistics contactsCollectionSlcs = sessionFactory().getStatistics().getSecondLevelCacheStatistics(Customer.class.getName() + ".contacts");
    assertEquals(1, contactsCollectionSlcs.getPutCount());
    assertEquals(1, contactsCollectionSlcs.getElementCountInMemory());
    assertEquals(1, contactsCollectionSlcs.getEntries().size());
    final Contact contact = addContact(customerId);
    assertNotNull("contact returned by addContact is null", contact);
    assertEquals("Customer.contacts cache was not invalidated after addContact", 0, contactsCollectionSlcs.getElementCountInMemory());
    assertNotNull("Contact missing after successful add call", getFirstContact(customerId));
    // read everyone's contacts
    readEveryonesFirstContact();
    removeContact(customerId);
    assertNull("contact still exists after successful remove call", getFirstContact(customerId));
}
Also used : Customer(org.hibernate.test.cache.infinispan.functional.entities.Customer) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Contact(org.hibernate.test.cache.infinispan.functional.entities.Contact) Test(org.junit.Test)

Example 4 with SecondLevelCacheStatistics

use of org.hibernate.stat.SecondLevelCacheStatistics 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 5 with SecondLevelCacheStatistics

use of org.hibernate.stat.SecondLevelCacheStatistics in project hibernate-orm by hibernate.

the class ReadWriteTest method testCollectionCache.

@Test
public void testCollectionCache() throws Exception {
    final Statistics stats = sessionFactory().getStatistics();
    stats.clear();
    final Item item = new Item("chris", "Chris's Item");
    final Item another = new Item("another", "Owned Item");
    item.addItem(another);
    withTxSession(s -> {
        s.persist(item);
        s.persist(another);
    });
    // The collection has been removed, but we can't add it again immediately using putFromLoad
    TIME_SERVICE.advance(1);
    withTxSession(s -> {
        Item loaded = s.load(Item.class, item.getId());
        assertEquals(1, loaded.getItems().size());
    });
    SecondLevelCacheStatistics cStats = stats.getSecondLevelCacheStatistics(Item.class.getName() + ".items");
    assertEquals(1, cStats.getElementCountInMemory());
    withTxSession(s -> {
        Item loadedWithCachedCollection = (Item) s.load(Item.class, item.getId());
        stats.logSummary();
        assertEquals(item.getName(), loadedWithCachedCollection.getName());
        assertEquals(item.getItems().size(), loadedWithCachedCollection.getItems().size());
        assertEquals(1, cStats.getHitCount());
        Map cacheEntries = cStats.getEntries();
        assertEquals(1, cacheEntries.size());
        Item itemElement = loadedWithCachedCollection.getItems().iterator().next();
        itemElement.setOwner(null);
        loadedWithCachedCollection.getItems().clear();
        s.delete(itemElement);
        s.delete(loadedWithCachedCollection);
    });
}
Also used : VersionedItem(org.hibernate.test.cache.infinispan.functional.entities.VersionedItem) Item(org.hibernate.test.cache.infinispan.functional.entities.Item) OtherItem(org.hibernate.test.cache.infinispan.functional.entities.OtherItem) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Statistics(org.hibernate.stat.Statistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Map(java.util.Map) Test(org.junit.Test)

Aggregations

SecondLevelCacheStatistics (org.hibernate.stat.SecondLevelCacheStatistics)32 Test (org.junit.Test)22 Statistics (org.hibernate.stat.Statistics)19 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 Map (java.util.Map)7 Session (org.hibernate.Session)7 NaturalIdCacheStatistics (org.hibernate.stat.NaturalIdCacheStatistics)5 QueryStatistics (org.hibernate.stat.QueryStatistics)5 EntityManager (javax.persistence.EntityManager)4 UserTransaction (javax.transaction.UserTransaction)4 Transaction (org.hibernate.Transaction)4 TestForIssue (org.hibernate.testing.TestForIssue)4 JPATest (org.jpwh.env.JPATest)4 Test (org.testng.annotations.Test)4 VersionedItem (org.hibernate.test.domain.VersionedItem)3 SessionFactory (org.hibernate.SessionFactory)2 CacheEntry (org.hibernate.cache.spi.entry.CacheEntry)2