Call us: +1-415-738-4000
If you are using persistent disk stores (Open Source), or distributed caching, care should be taken to shutdown Ehcache.
Note that Hibernate automatically shuts down its Ehcache CacheManager.
The recommended way to shutdown the Ehcache is:
CacheManager.shutdown()ShutdownListenerThough not recommended, Ehcache also lets you register a JVM shutdown hook.
Ehcache proivdes a ServletContextListener that shutsdown CacheManager. Use this when you want to shutdown Ehcache automatically when the web application is shutdown. To receive notification events, this class must be configured in the deployment descriptor for the web application. To do so, add the following to web.xml in your web application:
<listener>
<listener-class>net.sf.ehcache.constructs.web.ShutdownListener</listener-class>
</listener>
Ehcache CacheManager can optionally register a shutdown hook.
To do so, set the system property net.sf.ehcache.enableShutdownHook=true.
This will shutdown the CacheManager when it detects the Virtual Machine shutting down and
it is not already shut down.
Use the shutdown hook where:
Note: Shutdown hooks are inherently problematic. The JVM is shutting down, so sometimes things that can never be null are. Ehcache guards against as many of these as it can, but the shutdown hook should be the last option to use.
The shutdown hook is on CacheManager and simply calls the shutdown method. The sequence of events is:
call dispose for each Cache.
Each Cache will:
The shutdown hook runs when:
System.exit() is called, or the last non-daemon thread exits.kill -SIGTERM pid or kill -15 pid on Unix systems.The shutdown hook will not run when:
kill -SIGKILL pid or kill -9 pidTerminateProcess call is sent to the process on Windows systems.If shut down dirty, all in-memory data will be retained if Ehcache is configured for restartability. For more information, refer to Persistence and Restartability.
If using Open Source disk persistence when shut down dirty, then any persistent disk stores will be corrupted. They will be deleted, with a log message, on the next startup. Replications waiting to happen to other nodes in a distributed cache will also not get written.
