I\'m using a cache with disk store persistence. On subsequent reruns of the app I\'m getting the following error:
net.sf.ehcache.store.DiskStore deleteIndexI
Try setting the system property: net.sf.ehcache.enableShutdownHook=true
So, either you can add the following line in the beginning of your program:
System.setProperty("net.sf.ehcache.enableShutdownHook","true");
Or, from the command line to pass the property:
java -Dnet.sf.ehcache.enableShutdownHook=true ...
Note, the ehcache website does mention some caution when using this shutdown hook: Shutting Down Ehcache
When a shutdown hook will run, and when it will not
The shutdown hook runs when:
- a program exists normally. e.g. System.exit() is called, or the last non-daemon thread exits
- the Virtual Machine is terminated. e.g. CTRL-C. This corresponds to kill -SIGTERM pid or kill -15 pid on Unix systems.
The shutdown hook will not run when:
- the Virtual Machine aborts
- A SIGKILL signal is sent to the Virtual Machine process on Unix systems. e.g. kill -SIGKILL pid or kill -9 pid
- A TerminateProcess call is sent to the process on Windows systems.
Hope it works :)
For those of us using Ehcache with Spring 3.1+ and java config, you have to use:
@Bean(destroyMethod = "shutdown")
public net.sf.ehcache.CacheManager ehCacheManager() { .. }
and (assuming you're also using a non-web Spring ApplicationContext, enable the shutdown hook there to cause the bean to be destroyed gracefully:
context = new AnnotationConfigApplicationContext(AppConfig.class);
((AbstractApplicationContext) context).registerShutdownHook();
see this for more information.
How are you stopping your application?
From looking at the Ehcache code, it registers a JVM shutdown hook Runtime.getRuntime().addShutdownHook
that closes the cache on JVM exit. If the JVM is killed, or crashes, the shutdown hook will not be called.
Update RE your comment:
Here is the comment from the DiskStore dispose method:
Shuts down the disk store in preparation for cache shutdown
If a VM crash happens, the shutdown hook will not run. The data file and the index file will be out of synchronisation. At initialisation we always delete the index file after we have read the elements, so that it has a zero length. On a dirty restart, it still will have and the data file will automatically be deleted, thus preserving safety.
So, if your recreating the Cache in a different Unit test. Since the shutdownHook wouldn't have run, the index file will be 0. Ehcache thinks the index is corrupt. I would shutdown the Cache using JUnit's @After annotation in each of your unit tests. Or, share the Cache across all tests, but I'm guessing this would not give you isolated tests.