Ehcache disk store unclean shutdown

后端 未结 3 2010
轮回少年
轮回少年 2020-12-14 13:20

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         


        
3条回答
  •  佛祖请我去吃肉
    2020-12-14 14:03

    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.

提交回复
热议问题