简体   繁体   中英

Ehcache HIbernate confusing behaviour

I have implemented ehcache for a simple hibernate entity like below:

  @Bean(destroyMethod="shutdown")
  public net.sf.ehcache.CacheManager ehCacheManager() 
  {
    //default config
    CacheConfiguration cacheConfiguration = new CacheConfiguration();
    cacheConfiguration.setName("myCacheName");
    cacheConfiguration.setMemoryStoreEvictionPolicy("LRU");
    cacheConfiguration.setMaxEntriesLocalHeap(1000000);
    //cacheConfiguration.setOverflowToDisk(true);
    cacheConfiguration.setEternal(false);
    cacheConfiguration.setMaxEntriesLocalDisk(1000000);
    cacheConfiguration.setTimeToLiveSeconds(3600);
    cacheConfiguration.setStatistics(true);
    cacheConfiguration.addPersistence(new PersistenceConfiguration().strategy(Strategy.LOCALTEMPSWAP));

    net.sf.ehcache.config.Configuration config = new net.sf.ehcache.config.Configuration();
    config.addDefaultCache(cacheConfiguration);
    config.addDiskStore(new DiskStoreConfiguration().path("C:/myDiskStore"));

    //dynamic scenario cache
    net.sf.ehcache.Cache dynamicScenarioCache = new net.sf.ehcache.Cache(cacheConfiguration);
    dynamicScenarioCache.setName("dynamicScenario");

    //Ehcache manager
    net.sf.ehcache.CacheManager manager = net.sf.ehcache.CacheManager.create(config);
    manager.addCache(dynamicScenarioCache);
    //monitor stats
    MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
    ManagementService.registerMBeans(manager, mBeanServer, false, false, false, true);
      return manager;
   }

and then on my entity class I've got

@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "dynamicScenario")
@Entity
@Table(name="DynamicScenarioTable")
@JsonIgnoreProperties({/** to be specified */})
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "dynamicScenario")
public class DynamicScenario implements java.io.Serializable
  .....

and the query which I execute to fetch the data is:

 getSession().createQuery("from DynamicScenario").list();

it all works fine since in the jConsole I can see the number of memory hits and misses. However, one thing that worries me is that say when I manually enter a new entry in the table from the db then the number of entries is automatically updated in ehcache although I've set setTimeInterval of 3600 seconds which in my view means if any data is updated NOT FROM HIBERNATE but externally like I do it then hibernate would not know about it and make a call to the db up until the 3600 seconds have expired and clears the cache. So hibernate would not know that there's some change in the db since I have not inserted the data via hibernate. In addition to that, what bothers me is that I can still see in the console that hibernate executes a select statement to fetch the data even though I can see in the jConsole that these are registered as inMemoryHits and not db selects? Any ideas appreciated

When data is updated outside of Hibernate, the cache cannot know that so you can only rely on expiring data.

You probably see those selects when the items get expired or because the entities have been changed by some other concurrent transaction, so the cache entries got invalidated .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM