Search in sources :

Example 16 with SecondLevelCacheStatistics

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

the class EhCacheTest method testEmptySecondLevelCacheEntry.

@Test
public void testEmptySecondLevelCacheEntry() throws Exception {
    sessionFactory().getCache().evictEntityRegion(Item.class.getName());
    Statistics stats = sessionFactory().getStatistics();
    stats.clear();
    SecondLevelCacheStatistics statistics = stats.getSecondLevelCacheStatistics(Item.class.getName());
    Map cacheEntries = statistics.getEntries();
    assertEquals(0, cacheEntries.size());
}
Also used : SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Statistics(org.hibernate.stat.Statistics) Map(java.util.Map) Test(org.junit.Test)

Example 17 with SecondLevelCacheStatistics

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

the class ReadWriteTest method testAddNewOneToManyElementNoInitFlushInitLeaveCacheConsistent.

@Test
public void testAddNewOneToManyElementNoInitFlushInitLeaveCacheConsistent() throws Exception {
    Statistics stats = sessionFactory().getStatistics();
    stats.clear();
    SecondLevelCacheStatistics cStats = stats.getSecondLevelCacheStatistics(Item.class.getName() + ".items");
    ByRef<Long> itemId = new ByRef<>(null);
    saveItem(itemId);
    // create an element for item.bagOfItems
    Item itemElement = new Item();
    itemElement.setName("element");
    itemElement.setDescription("element item");
    withTxSession(s -> {
        Item item = s.get(Item.class, itemId.get());
        assertFalse(Hibernate.isInitialized(item.getBagOfItems()));
        item.addItemToBag(itemElement);
        assertFalse(Hibernate.isInitialized(item.getBagOfItems()));
        s.persist(itemElement);
        s.flush();
        Hibernate.initialize(item.getBagOfItems());
        markRollbackOnly(s);
    });
    withTxSession(s -> {
        Item item = s.get(Item.class, itemId.get());
        Hibernate.initialize(item.getBagOfItems());
        assertTrue(item.getBagOfItems().isEmpty());
        s.delete(item);
    });
}
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) ByRef(org.infinispan.commons.util.ByRef) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Statistics(org.hibernate.stat.Statistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Test(org.junit.Test)

Example 18 with SecondLevelCacheStatistics

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

the class ReadWriteTest method testAddNewOneToManyElementInitFlushLeaveCacheConsistent.

@Test
@TestForIssue(jiraKey = "HHH-9231")
public void testAddNewOneToManyElementInitFlushLeaveCacheConsistent() throws Exception {
    Statistics stats = sessionFactory().getStatistics();
    stats.clear();
    SecondLevelCacheStatistics cStats = stats.getSecondLevelCacheStatistics(Item.class.getName() + ".items");
    ByRef<Long> itemId = new ByRef<>(null);
    saveItem(itemId);
    // create an element for item.itsms
    Item itemElement = new Item();
    itemElement.setName("element");
    itemElement.setDescription("element item");
    withTxSession(s -> {
        Item item = s.get(Item.class, itemId.get());
        assertFalse(Hibernate.isInitialized(item.getItems()));
        item.addItem(itemElement);
        assertTrue(Hibernate.isInitialized(item.getItems()));
        s.persist(itemElement);
        s.flush();
        markRollbackOnly(s);
    });
    withTxSession(s -> {
        Item item = s.get(Item.class, itemId.get());
        Hibernate.initialize(item.getItems());
        assertTrue(item.getItems().isEmpty());
        s.delete(item);
    });
}
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) ByRef(org.infinispan.commons.util.ByRef) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Statistics(org.hibernate.stat.Statistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 19 with SecondLevelCacheStatistics

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

the class ReadWriteTest method testAddNewOneToManyElementNoInitFlushLeaveCacheConsistent.

@Test
@TestForIssue(jiraKey = "HHH-9231")
public void testAddNewOneToManyElementNoInitFlushLeaveCacheConsistent() throws Exception {
    Statistics stats = sessionFactory().getStatistics();
    stats.clear();
    SecondLevelCacheStatistics cStats = stats.getSecondLevelCacheStatistics(Item.class.getName() + ".items");
    ByRef<Long> itemId = new ByRef<>(null);
    saveItem(itemId);
    // create an element for item.bagOfItems
    Item itemElement = new Item();
    itemElement.setName("element");
    itemElement.setDescription("element item");
    withTxSession(s -> {
        Item item = s.get(Item.class, itemId.get());
        assertFalse(Hibernate.isInitialized(item.getItems()));
        item.addItemToBag(itemElement);
        assertFalse(Hibernate.isInitialized(item.getBagOfItems()));
        s.persist(itemElement);
        s.flush();
        markRollbackOnly(s);
    });
    withTxSession(s -> {
        Item item = s.get(Item.class, itemId.get());
        Hibernate.initialize(item.getItems());
        assertTrue(item.getItems().isEmpty());
        s.delete(item);
    });
}
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) ByRef(org.infinispan.commons.util.ByRef) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Statistics(org.hibernate.stat.Statistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 20 with SecondLevelCacheStatistics

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

the class BulkOperationsTest method testBulkOperations.

@Test
public void testBulkOperations() throws Throwable {
    boolean cleanedUp = false;
    try {
        createContacts();
        List<Integer> rhContacts = getContactsByCustomer("Red Hat");
        assertNotNull("Red Hat contacts exist", rhContacts);
        assertEquals("Created expected number of Red Hat contacts", 10, rhContacts.size());
        SecondLevelCacheStatistics contactSlcs = sessionFactory().getStatistics().getSecondLevelCacheStatistics(Contact.class.getName());
        assertEquals(20, contactSlcs.getElementCountInMemory());
        assertEquals("Deleted all Red Hat contacts", 10, deleteContacts());
        assertEquals(0, contactSlcs.getElementCountInMemory());
        List<Integer> jbContacts = getContactsByCustomer("JBoss");
        assertNotNull("JBoss contacts exist", jbContacts);
        assertEquals("JBoss contacts remain", 10, jbContacts.size());
        for (Integer id : rhContacts) {
            assertNull("Red Hat contact " + id + " cannot be retrieved", getContact(id));
        }
        rhContacts = getContactsByCustomer("Red Hat");
        if (rhContacts != null) {
            assertEquals("No Red Hat contacts remain", 0, rhContacts.size());
        }
        updateContacts("Kabir", "Updated");
        assertEquals(0, contactSlcs.getElementCountInMemory());
        for (Integer id : jbContacts) {
            Contact contact = getContact(id);
            assertNotNull("JBoss contact " + id + " exists", contact);
            String expected = ("Kabir".equals(contact.getName())) ? "Updated" : "2222";
            assertEquals("JBoss contact " + id + " has correct TLF", expected, contact.getTlf());
        }
        List<Integer> updated = getContactsByTLF("Updated");
        assertNotNull("Got updated contacts", updated);
        assertEquals("Updated contacts", 5, updated.size());
        assertEquals(10, contactSlcs.getElementCountInMemory());
        updateContactsWithOneManual("Kabir", "UpdatedAgain");
        assertEquals(0, contactSlcs.getElementCountInMemory());
        for (Integer id : jbContacts) {
            Contact contact = getContact(id);
            assertNotNull("JBoss contact " + id + " exists", contact);
            String expected = ("Kabir".equals(contact.getName())) ? "UpdatedAgain" : "2222";
            assertEquals("JBoss contact " + id + " has correct TLF", expected, contact.getTlf());
        }
        updated = getContactsByTLF("UpdatedAgain");
        assertNotNull("Got updated contacts", updated);
        assertEquals("Updated contacts", 5, updated.size());
    } catch (Throwable t) {
        cleanedUp = true;
        cleanup(true);
        throw t;
    } finally {
        // cleanup the db so we can run this test multiple times w/o restarting the cluster
        if (!cleanedUp) {
            cleanup(false);
        }
    }
}
Also used : SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Contact(org.hibernate.test.cache.infinispan.functional.entities.Contact) Test(org.junit.Test)

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