Search in sources :

Example 1 with NaturalIdCacheStatistics

use of org.hibernate.stat.NaturalIdCacheStatistics in project microservices by pwillhan.

the class SecondLevel method cacheNaturalId.

@Test
public void cacheNaturalId() throws Exception {
    CacheTestData testData = storeTestData();
    Long USER_ID = testData.users.getFirstId();
    Long ITEM_ID = testData.items.getFirstId();
    UserTransaction tx = TM.getUserTransaction();
    try {
        Statistics stats = JPA.getEntityManagerFactory().unwrap(SessionFactory.class).getStatistics();
        // Clear all natural ID cache regions
        JPA.getEntityManagerFactory().getCache().unwrap(org.hibernate.Cache.class).evictNaturalIdRegions();
        // Clear the User entity cache region
        JPA.getEntityManagerFactory().getCache().evict(User.class);
        {
            tx.begin();
            EntityManager em = JPA.createEntityManager();
            Session session = em.unwrap(Session.class);
            NaturalIdCacheStatistics userIdStats = stats.getNaturalIdCacheStatistics(User.class.getName() + "##NaturalId");
            assertEquals(userIdStats.getElementCountInMemory(), 0);
            User user = (User) session.byNaturalId(User.class).using("username", "johndoe").load();
            // select ID from USERS where USERNAME = ?
            // select * from USERS where ID = ?
            assertNotNull(user);
            assertEquals(userIdStats.getHitCount(), 0);
            assertEquals(userIdStats.getMissCount(), 1);
            assertEquals(userIdStats.getElementCountInMemory(), 1);
            SecondLevelCacheStatistics userStats = stats.getSecondLevelCacheStatistics(User.class.getName());
            assertEquals(userStats.getHitCount(), 0);
            assertEquals(userStats.getMissCount(), 1);
            assertEquals(userStats.getElementCountInMemory(), 1);
            tx.commit();
            em.close();
        }
        {
            // Execute the lookup again, hit the cache
            tx.begin();
            EntityManager em = JPA.createEntityManager();
            Session session = em.unwrap(Session.class);
            /* 
                   The natural identifier cache region for <code>User</code>s
                   has one element.
                 */
            NaturalIdCacheStatistics userIdStats = stats.getNaturalIdCacheStatistics(User.class.getName() + "##NaturalId");
            assertEquals(userIdStats.getElementCountInMemory(), 1);
            /* 
                   The <code>org.hibernate.Session</code> API performs natural
                   identifier lookup; this is the only API for accessing the
                   natural identifier cache.
                 */
            User user = (User) session.byNaturalId(User.class).using("username", "johndoe").load();
            assertNotNull(user);
            /* 
                   You had a cache hit for the natural identifier lookup; the
                   cache returned the identifier value of "johndoe".
                 */
            assertEquals(userIdStats.getHitCount(), 1);
            /* 
                   You also had a cache hit for the actual entity data of
                   that <code>User</code>.
                 */
            SecondLevelCacheStatistics userStats = stats.getSecondLevelCacheStatistics(User.class.getName());
            assertEquals(userStats.getHitCount(), 1);
            tx.commit();
            em.close();
        }
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) SessionFactory(org.hibernate.SessionFactory) EntityManager(javax.persistence.EntityManager) NaturalIdCacheStatistics(org.hibernate.stat.NaturalIdCacheStatistics) User(org.jpwh.model.cache.User) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Statistics(org.hibernate.stat.Statistics) QueryStatistics(org.hibernate.stat.QueryStatistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) NaturalIdCacheStatistics(org.hibernate.stat.NaturalIdCacheStatistics) Cache(javax.persistence.Cache) Session(org.hibernate.Session) Test(org.testng.annotations.Test) JPATest(org.jpwh.env.JPATest)

Example 2 with NaturalIdCacheStatistics

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

the class RegionNameTest method testLegacyStatsApi.

// todo (5.3) : any other API I can think of that deals with region-name?
// todo (6.0) : same ^^, maintain API compatibility
@Test
public void testLegacyStatsApi() {
    // these references need to be the prefixed name
    final String regionName = cachePrefix + '.' + localName;
    final Statistics stats = sessionFactory().getStatistics();
    assertEquals(2, stats.getSecondLevelCacheRegionNames().length);
    final SecondLevelCacheStatistics secondLevelCacheStatistics = stats.getSecondLevelCacheStatistics(regionName);
    assert secondLevelCacheStatistics != null;
    final NaturalIdCacheStatistics naturalIdCacheStatistics = stats.getNaturalIdCacheStatistics(regionName);
    assert naturalIdCacheStatistics != null;
}
Also used : NaturalIdCacheStatistics(org.hibernate.stat.NaturalIdCacheStatistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Statistics(org.hibernate.stat.Statistics) NaturalIdCacheStatistics(org.hibernate.stat.NaturalIdCacheStatistics) Test(org.junit.Test)

Example 3 with NaturalIdCacheStatistics

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

the class CachedMutableNaturalIdStrictReadWriteTest method testNaturalIdCacheStatisticsReset.

@Test
@TestForIssue(jiraKey = "HHH-9200")
public void testNaturalIdCacheStatisticsReset() {
    final String naturalIdCacheRegion = "hibernate.test.org.hibernate.test.naturalid.mutable.cached.Another##NaturalId";
    sessionFactory().getStatistics().clear();
    Session session = openSession();
    session.beginTransaction();
    final Another it = new Another("IT");
    session.save(it);
    session.getTransaction().commit();
    session.close();
    NaturalIdCacheStatistics statistics = sessionFactory().getStatistics().getNaturalIdCacheStatistics(naturalIdCacheRegion);
    assertEquals(1, statistics.getPutCount());
    sessionFactory().getStatistics().clear();
    // Refresh statistics reference.
    statistics = sessionFactory().getStatistics().getNaturalIdCacheStatistics(naturalIdCacheRegion);
    assertEquals(0, statistics.getPutCount());
    session = openSession();
    session.beginTransaction();
    session.delete(it);
    session.getTransaction().commit();
    session.clear();
}
Also used : NaturalIdCacheStatistics(org.hibernate.stat.NaturalIdCacheStatistics) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 4 with NaturalIdCacheStatistics

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

the class CachedMutableNaturalIdStrictReadWriteTest method testToMapConversion.

@Test
@TestForIssue(jiraKey = "HHH-9203")
public void testToMapConversion() {
    sessionFactory().getStatistics().clear();
    final Session session = openSession();
    session.getTransaction().begin();
    final AllCached it = new AllCached("IT");
    session.save(it);
    session.getTransaction().commit();
    session.close();
    final NaturalIdCacheStatistics stats = sessionFactory().getStatistics().getNaturalIdCacheStatistics("hibernate.test." + AllCached.class.getName() + "##NaturalId");
    assertEquals(1, stats.getPutCount());
}
Also used : NaturalIdCacheStatistics(org.hibernate.stat.NaturalIdCacheStatistics) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

NaturalIdCacheStatistics (org.hibernate.stat.NaturalIdCacheStatistics)4 Session (org.hibernate.Session)3 Test (org.junit.Test)3 SecondLevelCacheStatistics (org.hibernate.stat.SecondLevelCacheStatistics)2 Statistics (org.hibernate.stat.Statistics)2 TestForIssue (org.hibernate.testing.TestForIssue)2 Cache (javax.persistence.Cache)1 EntityManager (javax.persistence.EntityManager)1 UserTransaction (javax.transaction.UserTransaction)1 SessionFactory (org.hibernate.SessionFactory)1 QueryStatistics (org.hibernate.stat.QueryStatistics)1 JPATest (org.jpwh.env.JPATest)1 User (org.jpwh.model.cache.User)1 Test (org.testng.annotations.Test)1