4 Replies Latest reply on May 27, 2016 12:21 AM by mjhaller

    How to get CacheManager Instance in Hibernate

    glsgolden

      Hi all,

       

      Our application in currently using the EHCache. we are planning to migrate from EHCache to Infinispan cache.

      EHCache has provider like SingletonEhCacheProvider using this we sure that there is always only one CacheManager for whole application.

       

      how to get the singleton CacheManger in infinispan?

       

      for example

       

      EHcache :

           ehcahce provider : net.sf.ehcache.hibernate.SingletonEhCacheProvider

      CacheManager Instance:

       

      CacheManger cacheManager = CacheManager.getInstance();

       

      using that we can get the cacheManager Object.

       

      Thanks,

      Ganesh Shirsat

        • 1. How to get CacheManager Instance in Hibernate
          galder.zamarreno

          Assuming that you're using EHCache for 2nd level caching, Hibernate does not provide an standard way to get hold of the underlying cache provider, and so you cannot get hold of the Infinispan CacheManager. I can assure you (I wrote the Infinispan 2LC code ) that a only a single cache manager is used for maintaining the Hibernate 2LC.

          • 2. How to get CacheManager Instance in Hibernate
            glsgolden

            thanks

            • 3. Re: How to get CacheManager Instance in Hibernate
              mjhaller

              Is this still true?  I see in other posts one isn't supposed to fiddle with the CacheManager, but I see valid use cases for getting information about individual cached ids.

               

              When determining the health of the 2LC for testing purposes, it is useful to be able to look at individual cache entries.  For example, I was looping through the cache.getKeys() with EhCache:

               

              CacheKey cacheKey = (CacheKey) key;

              Element element = cache.getQuiet(key);

              // fields like this are useful to print for debugging

              cacheKey.getKey().toString()

              element.getHitCount();

              element.getLastAccessTime();

              element.getLastUpdateTime();

              element.getCreationTime();

              • 4. Re: How to get CacheManager Instance in Hibernate
                mjhaller

                Nevermind.  All this is available from the hibernate l2 stats:

                 

                 

                  Statistics statistics = entityManager.getSession().getSessionFactory().getStatistics();

                  statistics.setStatisticsEnabled(true);

                  SecondLevelCacheStatistics l2cacheStatsstatistics.getSecondLevelCacheStatistics(cacheName);