Ehcache Manager - spring

I have an application that use JDBCTemplate and Hibernate.
Both of this configure to use ehcache but exception thrown:
org.hibernate.cache.CacheException: net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following:
JDBCTemplate configured using spring xml
Hibernate configured using hibernate.cfg.xml
Please help.
I tried to disable the default cache in ehcache.xml but failed and i have the shared property set to true which also caused fail.
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false">
<diskStore path="java.io.tmpdir" />
<!--<defaultCache eternal="true" maxElementsInMemory="50000"
overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
timeToLiveSeconds="0" memoryStoreEvictionPolicy="LRU"
/>
-->
<cache name="genericDao" eternal="false" maxElementsInMemory="50000"
overflowToDisk="false" memoryStoreEvictionPolicy="LFU" />
<cache name="reportDao" eternal="false" maxElementsInMemory="50000"
timeToIdleSeconds="28800" timeToLiveSeconds="28800"
overflowToDisk="false" memoryStoreEvictionPolicy="LFU" />
</ehcache>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="WEB-INF/ehcache/ehcache.xml"></property>
<property name="shared" value="true"></property>
</bean>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcache"></property>
</bean>
How to provide unique name in this case ?
Thanks.

Create two ehcache.xml configuration with different names.

Related

Configure EHCache with Spring and Hibernate, using multiple cache managers

(first of all, sorry for my english ... I don't speak it very well)
Hi,
I'm developing a multi-module web app using spring framework in version 4.0.9. I have three different jar modules containing three different entity models wired to its own database each other. These three models are using Hibernate in version 4.3.5 and spring too.
I want to configure Ehcache as hibernate's second level cache on each of one jar model modules and likewise I need to configure Ehcache for cache provider at principal webapp project.
model-1.jar (spring + hibernate + ehcache), with hibernate-ehcache dependency version 4.3.5
model-2.jar (spring + hibernate + ehcache), with hibernate-ehcache dependency version 4.3.5
model-3.jar (spring + hibernate + ehcache), with hibernate-ehcache dependency version 4.3.5
webapp.war (spring-mvc + ehcache), with spring-context-support 4.0.9 version dependency
In model-1 module something like this:
(model-1-spring-context.xml)
<bean id="oneSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" lazy-init="true">
<property name="dataSource" ref="oneDataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="net.sf.ehcache.configurationResourceName">one-ehcache.xml</prop>
</props>
</property>
<property name="packagesToScan" value="......" />
</bean>
(one-ehcache.xml)
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd" name="oneHibernateCache">
<diskStore path="/tmp/xxxx"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
</ehcache>
model-2 and model-3 with analogous structure to model-1.
And webapp configuration something like this:
(class configuration)
#EnableCaching
#Configuration
public class CacheConfig {
#Bean
public CacheManager cacheManager() {
return new EhCacheCacheManager(ehCacheCacheManager().getObject());
}
#Bean
public EhCacheManagerFactoryBean ehCacheCacheManager() {
EhCacheManagerFactoryBean factory = new EhCacheManagerFactoryBean();
factory.setConfigLocation(new ClassPathResource("web-ehcache.xml"));
factory.setShared(true);
return factory;
}
}
(web-cache.xml)
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" name="webSpringCache">
<diskStore path="/tmp/zzzzz" />
<defaultCache
maxElementsInMemory="1"
eternal="true"
overflowToDisk="false"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="1800"
memoryStoreEvictionPolicy="FIFO">
</defaultCache>
I'm having several problems with cache managers, cache names and regions inside them. Spring context doesn't start ok and I don't know how to configure properly the modules.
I'd very pleased if somebody could help me.
Thanks a lot to everybody.

hibernate second level cache keeps emptying itself

Have a Spring/Tomcat application and I'm trying to get ehcache working as Hibernate second-level entity cache. It is enabled and active. I can see in the debug log output how many entities are cached. The problem is that every few seconds, the cache empties. I even pinned the cache (just to see if that would alter the behavior) and now I see this:
05-08-2017 16:05:21.550 [taskExecutor-12] {env=''} WARN n.s.e.Cache: Data availability impacted:
****************************************************************************************
************************** removeAll called on a pinned cache **************************
05-08-2017 16:05:21.550 [taskExecutor-12] {env=''} WARN n.s.e.Cache: Data availability impacted:
****************************************************************************************
************************** removeAll called on a pinned cache **************************
So I think this is the problem. Any idea what would cause removeAll to be implicitly called over and over?
ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="true"
monitoring="autodetect"
maxBytesLocalHeap="24G"
dynamicConfig="true">
<defaultCache eternal="true" maxBytesLocalHeap="1" /> <!-- not used, but required per ehcache configuration rules -->
<!-- cache for foo -->
<cache name="foo"
maxBytesLocalHeap="8G"
eternal="false"
memoryStoreEvictionPolicy="LRU"
timeToLiveSeconds="12000"
transactionalMode="off">
<pinning store="inCache" />
<persistence strategy="none" />
</cache>
</ehcache>
persistence.xml
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy" />
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider" />
<property name="hibernate.cache.use_second_level_cache" value="true" />
<property name="hibernate.cache.use_query_cache" value="false" />
<property name="hibernate.generate_statistics" value="true" />
<property name="hibernate.cache.use_structured_entries" value="true" />
<property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory" />
</properties>
And these annotations are on the entity model class:
#Cacheable
#Cache(region = "foo", usage = CacheConcurrencyStrategy.READ_WRITE)
I figure it out. Native SQL DELETE queries were causing hibernate to frequently dump the entire cache (the removeAll calls above). I did as suggested below, and everything is working now.
2nd level cache invalidation with native queries

ehcache diskStore is not working

my directory of ehcache is not been created whe I set the property . I am using ehcache-core 2.6.9 and hibernate-ehcache 4.0.1.Final
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
monitoring="autodetect" dynamicConfig="true">
<diskStore path="/tmp/ehcacheApp1" />
<defaultCache maxEntriesLocalHeap="10000" eternal="false"
timeToIdleSeconds="0" timeToLiveSeconds="1800" diskSpoolBufferSizeMB="80"
maxEntriesLocalDisk="10000000" diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU" statistics="true">
<persistence strategy="localTempSwap" />
</defaultCache>
<cache name="blockiscsi" maxEntriesLocalHeap="10000" eternal="false"
timeToIdleSeconds="5" timeToLiveSeconds="10">
<persistence strategy="localTempSwap" />
</cache>
<cache name="org.hibernate.cache.internal.StandardQueryCache"
maxEntriesLocalHeap="5" eternal="false" timeToLiveSeconds="120">
<persistence strategy="localTempSwap" />
</cache>
<cache name="org.hibernate.cache.spi.UpdateTimestampsCache"
maxEntriesLocalHeap="5000" eternal="true">
<persistence strategy="localTempSwap" />
</cache>
</ehcache>
update: I also tried this: <diskStore path="user.dir/ehcache" />
I received this log:
17:53:31.232 [main] WARN net.sf.ehcache.DiskStorePathManager - diskStorePath '/tmp/ehcache' is already used by an existing CacheManager either in the same VM or in a different process.
The diskStore path for this CacheManager will be set to /tmp/ehcache/ehcache_auto_created1431805507138728176diskstore.
To avoid this warning consider using the CacheManager factory methods to create a singleton CacheManager or specifying a separate ehcache configuration (ehcache.xml) for each CacheManager instance.
try with SingletonEhCacheProvider setting this
<property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheP‌​rovider</property>

Load webserver context.xml using Spring

Fairly new to Spring, so I'm having some trouble with this. I'm trying to use LDAP security with Spring. I can use a properties file I created inside the webapp itself. But what I would like to do is load and read the context.xml file of the server (it has all the values I need for this and other applications).
This is what I have:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
<bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="searchContextAttributes" value="true"/>
<property name="contextOverride" value="true"/>
<property name="ignoreResourceNotFound" value="true"/>
<property name="locations">
<list>
<value>/WEB-INF/properties/dataUploadProperties.properties</value>
<value>/WEB-INF/properties/globalProperties.properties</value>
<value>context.xml</value>
</list>
</property>
</bean>
I'm able to load and read the 2 properties files, but the context.xml is not found. Does it need to be the absolute path on the server?
Thanks
Chris
So the first thing I would recommend is to use Spring Security. It has an already build in LDAP support.
but the context.xml is not found
Normally this (reading the context.xml directly) is not the way you should go.
Instead, define some properties and or JNDI resources in the context.xml and then use them in the spring configuration.
For example:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
<!-- access via jndi -->
<jee:jndi-lookup id="jndiEmailSession"
jndi-name="java:comp/env/email/session/myEmailSession" />
<!-- direct access for properties required the SERVLET contect property
place older configurer, then it works like properties from normal
property files -->
<bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer"> <property name="locations" value="classpath*:META-INF/spring/*.properties" /> </bean>
<bean class=Demo>
<property name="someString" value="${simpleValue}" />
</bean>
</beans>
context.xml:
<Resource name="email/session/myEmailSession"
type="javax.mail.Session"
auth="Container"
password="secret"
mail.debug="false"
mail.transport.protocol="smtp"
mail.smtp.auth="true"
mail.smtp.user="test#example.com"
mail.smtp.host="mail.example.com"
mail.smtp.from="test#example.com"/>
<Parameter name="simpleValue" value="any" override="false" />

How to disable the Ehcache update checker?

12:18:55,541 INFO [UpdateChecker] New update(s) found: 2.0.0 [http://ehcache.org/news.html]
How do I suppress ehcache checking for new update(s), this is happening while loading my j2ee application and when ehcache is getting initialized.
One way is to place a ehcache.xml file on your classpath with the attribute updateCheck="false" in the root tag.
For example:
<ehcache updateCheck="false">
<defaultCache
maxElementsInMemory="0"
eternal="false"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
overflowToDisk="false"
diskPersistent="false"/>
</ehcache>
Another way is to set an environment variable:
System.setProperty("net.sf.ehcache.skipUpdateCheck", "true");
simply put, in your ehcache.xml configuration file, make sure you disable the updateCheck :
<ehcache updateCheck="false">
<defaultCache
maxElementsInMemory="0"
eternal="false"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
overflowToDisk="false"
diskPersistent="false"
/>
</ehcache>

Resources