Search in sources :

Example 1 with Cache

use of javax.persistence.Cache in project microservices by pwillhan.

the class SecondLevel method cacheControl.

@Test
public void cacheControl() throws Exception {
    CacheTestData testData = storeTestData();
    Long USER_ID = testData.users.getFirstId();
    Long ITEM_ID = testData.items.getFirstId();
    EntityManagerFactory emf = JPA.getEntityManagerFactory();
    Cache cache = emf.getCache();
    assertTrue(cache.contains(Item.class, ITEM_ID));
    cache.evict(Item.class, ITEM_ID);
    cache.evict(Item.class);
    cache.evictAll();
    org.hibernate.Cache hibernateCache = cache.unwrap(org.hibernate.Cache.class);
    assertFalse(hibernateCache.containsEntity(Item.class, ITEM_ID));
    hibernateCache.evictEntityRegions();
    hibernateCache.evictCollectionRegions();
    hibernateCache.evictNaturalIdRegions();
    hibernateCache.evictQueryRegions();
}
Also used : Item(org.jpwh.model.cache.Item) EntityManagerFactory(javax.persistence.EntityManagerFactory) Cache(javax.persistence.Cache) Test(org.testng.annotations.Test) JPATest(org.jpwh.env.JPATest)

Example 2 with Cache

use of javax.persistence.Cache 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 3 with Cache

use of javax.persistence.Cache in project OpenOLAT by OpenOLAT.

the class DBImpl method getCacheContainer.

@Override
public EmbeddedCacheManager getCacheContainer() {
    EmbeddedCacheManager cm;
    try {
        Cache cache = emf.getCache();
        JpaInfinispanRegionFactory region = cache.unwrap(JpaInfinispanRegionFactory.class);
        cm = region.getCacheManager();
    } catch (Exception e) {
        log.error("", e);
        cm = null;
    }
    return cm;
}
Also used : EmbeddedCacheManager(org.infinispan.manager.EmbeddedCacheManager) AssertException(org.olat.core.logging.AssertException) DBRuntimeException(org.olat.core.logging.DBRuntimeException) SQLException(java.sql.SQLException) RollbackException(javax.persistence.RollbackException) HibernateException(org.hibernate.HibernateException) Cache(javax.persistence.Cache)

Example 4 with Cache

use of javax.persistence.Cache in project openolat by klemens.

the class DBImpl method getCacheContainer.

@Override
public EmbeddedCacheManager getCacheContainer() {
    EmbeddedCacheManager cm;
    try {
        Cache cache = emf.getCache();
        JpaInfinispanRegionFactory region = cache.unwrap(JpaInfinispanRegionFactory.class);
        cm = region.getCacheManager();
    } catch (Exception e) {
        log.error("", e);
        cm = null;
    }
    return cm;
}
Also used : EmbeddedCacheManager(org.infinispan.manager.EmbeddedCacheManager) AssertException(org.olat.core.logging.AssertException) DBRuntimeException(org.olat.core.logging.DBRuntimeException) SQLException(java.sql.SQLException) RollbackException(javax.persistence.RollbackException) HibernateException(org.hibernate.HibernateException) Cache(javax.persistence.Cache)

Aggregations

Cache (javax.persistence.Cache)4 SQLException (java.sql.SQLException)2 RollbackException (javax.persistence.RollbackException)2 HibernateException (org.hibernate.HibernateException)2 EmbeddedCacheManager (org.infinispan.manager.EmbeddedCacheManager)2 JPATest (org.jpwh.env.JPATest)2 AssertException (org.olat.core.logging.AssertException)2 DBRuntimeException (org.olat.core.logging.DBRuntimeException)2 Test (org.testng.annotations.Test)2 EntityManager (javax.persistence.EntityManager)1 EntityManagerFactory (javax.persistence.EntityManagerFactory)1 UserTransaction (javax.transaction.UserTransaction)1 Session (org.hibernate.Session)1 SessionFactory (org.hibernate.SessionFactory)1 NaturalIdCacheStatistics (org.hibernate.stat.NaturalIdCacheStatistics)1 QueryStatistics (org.hibernate.stat.QueryStatistics)1 SecondLevelCacheStatistics (org.hibernate.stat.SecondLevelCacheStatistics)1 Statistics (org.hibernate.stat.Statistics)1 Item (org.jpwh.model.cache.Item)1 User (org.jpwh.model.cache.User)1