tags:

views:

235

answers:

1

I have an ehcache setup, that works just fine, except that the persistent diskstore data is removed everytime I restart my application/server (Spring application on TcServer/Tomcat). The whole point of using the persistent diskstore was to be able to persist the cache in spite of application restarts.

Here is my ehcache.xml

<?xml version="1.0" encoding="UTF-8"?><ehcache><diskStore path="java.io.tmpdir/ehcache"/><cache name="clusterCache"
  maxElementsInMemory="1"
  maxElementsOnDisk="50"
  eternal="true"
  overflowToDisk="true"
  diskPersistent="true" 
  memoryStoreEvictionPolicy="LFU"/></ehcache>

Any ideas why this is happening?

+2  A: 

As the documentation notes, Ehcache doesn't write the index file that it looks for to restore from the disk unless the cache is properly shutdown, either via a VM shutdown hook or (since this is in a servlet container) the servlet context listener it provides. See the Ehcache documentation on shutting down for details, though it just means adding the below to your web.xml:

<listener>
      <listener-class>net.sf.ehcache.constructs.web.ShutdownListener</listener-class>
</listener> 
ig0774
Hi, I just found this a few minutes ago, and I did just that and i put it right at the top of my web.xml - <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"><listener><listener-class>net.sf.ehcache.constructs.web.ShutdownListener</listener-class></listener>but it's still clearing it, with the same message. When I use the cache, I can see that the data file..
chrismarx
has data (94kb), but the index file is always empty, could that be the problem?
chrismarx
Do you get the .index files created? If not, try flushing the cache manually at some point during application execution and see if anything gets written to disk.
ig0774
yes, the index file is created, but remains empty-
chrismarx
@chrismarx: My response before I saw the second. Without the index files getting data, ehcache will default to deleting the disk store. Try manually flushing, e.g. myCache.flush() at some point.
ig0774
hi, well that worked! but the question is why is this necessary at all? if indeed i dont find out the root cause, what would be your advice as to when to call the flush method? For the moment, I just stuck it into the service method, so flush is called after each time i make a request for my cached object-
chrismarx
@chrismarx: It shouldn't be necessary to do that as the servlet listener ought to pick-up on the shutdown. Are you potentially killing the JVM in way that would cause unclean shutdown of your app server (e.g., ctrl+c or something along those lines)? Try enabling DEBUG level logging for net.sf.ehcache.constructs.web.ShutdownListener. You should see a message on shutdown.
ig0774
i enabled a rolling file logger, so i'd be sure not miss shutdown logging, added this: <logger name="net.sf.ehcache.constructs.web"> <level value="debug" /> </logger>and there are plenty of logs from everything else, including other echache logs, but nothing from this. Would the position of the listener in the web.xml maybe cause problems?
chrismarx
i found this answer - http://stackoverflow.com/questions/2373431/ehcache-disk-store-unclean-shutdownand it mentioned explicitly calling CacheManager.shutdown() before the application shuts down. I set up an application event listener for spring, and listened for the ContextClosedEvent and made that call. After that call is made, the log says that the cachemanager is already shutdown, but low and behold, the persistent disk cache has managed to not be clear after many restarts now. hopefully this will be the permanent answer (or at least till a better on comes along)
chrismarx