6

I have enabled 2nd level caching in Hibernate 4.3.11 by adding:

 config.setProperty("hibernate.cache.region.factory_class", "org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory");
 config.setProperty("hibernate.cache.use_second_level_cache", "true");

to my Hibernate Config.

This to my pom.xml (Not sure if necessary for pom definition to be this awkward)

<dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-ehcache</artifactId>
      <version>4.3.11.Final</version>
      <exclusions>
          <exclusion>
              <groupId>net.sf.ehcache</groupId>
              <artifactId>ehcache-core</artifactId>
          </exclusion>
      </exclusions>
      </dependency>
      <dependency>
          <groupId>net.sf.ehcache</groupId>
          <artifactId>ehcache</artifactId>
          <version>2.7.0</version>
      </dependency>

and this to the class I want to cache

@Cacheable
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)

But how do I configure the cache size in code when the database is created, its not practical for me to use Xml file that just adds complication to the build process I would much prefer to do in code.

Update, after creating database from Hibernate, I find the Caches are already created

CacheManager.create();
String[] cacheNames = CacheManager.getInstance().getCacheNames();
for(String cacheName:cacheNames)
{
    MainWindow.logger.severe("CacheName:"+cacheName);
    Cache cache = CacheManager.getInstance().getCache(cacheName);
    cache.getCacheConfiguration().setMaxEntriesInCache(1000);
    cache.getCacheConfiguration().setLogging(true);
}

but how can I affect how they are created or does modifying values like i have done is enough to update. When I run I see no debugging outout or anything to indicate the cache is being used.

Paul Taylor
  • 13,411
  • 42
  • 184
  • 351
  • Firstly don't use EhCache 2 but EhCache 3. You can expect that EhCache 2 usage and support will slowly disappear. – cruftex Oct 17 '18 at 10:05
  • For EhCache I cannot answer the actual question instanly. I am the author of cache2k. If you like, I can put together a guide on how to use cache2k with hibernate. – cruftex Oct 17 '18 at 10:09
  • It would be interesting why the XML file is troublesome for you. Maybe you'd like it derive it from other values in your applications configuration? In cache2k I have at least some mechanisms that you can use/set values via system properties. – cruftex Oct 17 '18 at 10:13
  • Well I assume it can be either a standalone file in the working directory or can be within a jar on main class path. But in either case I would have to modify my build scripts, and if only allowedmstandalone file would have to check on each startup file existed otherwise cache would be unlimited and use all memory ! – Paul Taylor Oct 17 '18 at 10:47
  • I use a configuration class to build by Hibernate Class, and I can see you can use CacheManager.getInstance() but this is AFTER hibernate has already created cache. I'm not particulary tied to Ehcache this is just the default, it seems perhaps I can use JCache (didnt know about this) but the key thing whatever I use is is there a way to effect how Hibernate creates it caches – Paul Taylor Oct 17 '18 at 10:51

1 Answers1

1

You could subclass org.hibernate.cache.ehcache.EhCacheRegionFactory and perform any cache configuration manually, and then tell Hibernate to use your custom cache factory with:

Configuration.setProperty("hibernate.cache.region.factory_class", "my.cache.FactoryClass");

See: http://www.ehcache.org/documentation/2.7/integrations/hibernate.html#set-the-hibernate-cache-provider-programmatically-

marcprux
  • 9,845
  • 3
  • 55
  • 72
  • okay i looked at this and didnt seem straight forward to provide an alternative class that did this. So in the end I reluncantly decided to use ehcache.xml instead which works in dev, but gives me more work to do when building production version. – Paul Taylor Oct 29 '18 at 17:41