-
@Cacheable is ignored
Hi all I'm trying to use Spring Cache Abstraction in my project but probably I'm doing something wrong. It seems that @Cacheable annotation in my dao class is ignored. I'm using Spring 3.1.0 and ehcache 2.6.0
Here is my spring context configuration file:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:oxm="http://www.springframework.org/schema/oxm"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util" xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<!-- Ehcache configuration -->
<cache:annotation-driven/>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcache" />
<!-- Ehcache library setup -->
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:config-location="classpath:ehcacheSpring.xml" />
...
...
...
<!-- DAOs -->
<bean id="marketingDao" class="it.iside.profilogestore.dao.spring.MarketingDaoImpl">
<property name="dataSource" ref="dataSource" />
<property name="sqlManager" ref="sqlManager" />
<property name="applicationProperties" ref="applicationProperties"></property>
</bean>
</beans>
Here is my Dao class with @cacheable annotation:
Code:
package it.iside.profilogestore.dao.spring;
import it.iside.commongwt.exception.GwtException;
import it.iside.jdbc.XmlSqlManager;
import it.iside.jdbc.spring.AutoBindRowMapper;
import it.iside.logging.Loggable;
import it.iside.profilogestore.common.dto.MktSettori;
import it.iside.profilogestore.common.dto.SettoriParam;
import it.iside.profilogestore.dao.MarketingDao;
import java.util.List;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcDaoSupport;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
@Loggable
public class MarketingDaoImpl extends NamedParameterJdbcDaoSupport implements MarketingDao, MessageSourceAware {
private Logger logger;
private MessageSource messageSource;
private XmlSqlManager sqlManager;
private Properties applicationProperties;
public void setMessageSource(MessageSource messageSource) {
this.messageSource = messageSource;
}
/**
* @param sqlManager
* the sqlManager to set
*/
public void setSqlManager(XmlSqlManager sqlManager) {
this.sqlManager = sqlManager;
}
/**
* @param applicationProperties the applicationProperties to set
*/
public void setApplicationProperties(Properties applicationProperties) {
this.applicationProperties = applicationProperties;
}
@Cacheable(value="NdgsList", key="#param.codSettore")
public List<String> findNdgsFromSettore(SettoriParam param) throws GwtException {
try {
if(param.getkAbi() == null || param.getCodSettore() == null)
throw new GwtException(messageSource.getMessage("paramsException", new Object[]{param.toString()}, null));
String sql = sqlManager.getSqlStatement("findNdgsFromSettore").getSql();
SqlParameterSource namedParameters = new BeanPropertySqlParameterSource(param);
logger.debug(param);
logger.debug(sql.replaceAll("\n", " "));
List<String> res = getNamedParameterJdbcTemplate().queryForList(sql, namedParameters, String.class);
logger.debug("Returned " + res.size() + " records for "+ param.getkAbi()+","+param.getCodSettore());
return res;
} catch (Exception e) {
e.printStackTrace();
throw new GwtException(e);
}
}
}
here is service class where method sign with annotation is invoked:
Code:
package it.iside.profilogestore.frontend.server.service;
import it.iside.commongwt.exception.GwtException;
import it.iside.logging.Loggable;
import it.iside.profilobase.common.dto.operativita.OperativitaDetailRow;
import it.iside.profilobase.common.dto.operativita.OperativitaParam;
import it.iside.profilogestore.common.dto.SettoriParam;
import it.iside.profilogestore.dao.MarketingDao;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
@SuppressWarnings("serial")
@Loggable
public class OperativitaServiceImpl extends it.iside.profilobase.common.server.service.OperativitaServiceImpl {
@Autowired
private MarketingDao marketingDao;
/**
* @param marketingDao the marketingDao to set
*/
public void setMarketingDao(MarketingDao marketingDao) {
this.marketingDao = marketingDao;
}
@Override
public List<OperativitaDetailRow> findDatiStatistici(OperativitaParam param) throws GwtException {
String ndgsList = "";
try {
SettoriParam sParam = new SettoriParam();
sParam.setCodSettore(param.getGestore());
sParam.setkAbi(param.getkAbi());
ndgsList = findNdgsFromSettore(sParam);
Map<String, String> variables = new HashMap<String, String>();
variables.put("LISTA_NAG", ndgsList);
variables.put("LISTA_NAG_1", ndgsList);
variables.put("LISTA_NAG_2", ndgsList);
param.setSqlVariables(variables);
} catch (Exception e) {
logger.error(e);
throw new GwtException(messageSource.getMessage("operativitaException", null, null), e);
}
return super.findDatiStatistici(param);
}
private String findNdgsFromSettore(SettoriParam param) throws GwtException {
String result = StringUtils.join(marketingDao.findNdgsFromSettore(param).iterator(), ",");
return result;
}
}
and here is ehcache configuration file:
Code:
<ehcache>
<diskStore path="/home/agiantin/cacheData/spring"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
/>
<cache name="NdgsList"
maxElementsInMemory="1"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="600"
timeToLiveSeconds="600"
/>
</ehcache>
Every time method findNdgsFromSettore is called it is executed and I can't see anything in logs and cache file never grow... as I said above @cacheable annotation is ignored.
The method with annotation is public, the class implements an interface with that method signature, the method is called from an external class and spring initialize cacheManager properly on application init...
Code:
org.springframework.cache.ehcache.EhCacheManagerFactoryBean afterPropertiesSet Initializing EHCache CacheManager
org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@58f058f0: defining beans [applicationProperties,messageSource,log4jInitializer,it.iside.logging.LoggerBeanFactoryPostProcessor#0,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.cache.annotation.AnnotationCacheOperationSource#0,org.springframework.cache.interceptor.CacheInterceptor#0,org.springframework.cache.config.internalCacheAdvisor,cacheManager,ehcache,dataSource,sadasDataSource,it.iside.logging.LoggerBeanFactoryPostProcessor#1,sqlManager,operativitaBO,marketingDao,operativitaDao,anomalieDao,anagNagDao,centraleRischiDao,redditivitaDao]; root of factory hierarchy
so what's wrong?
Thank you so much for replies.
-
Quote:
Originally Posted by
Giant
Code:
<ehcache>
<diskStore path="/home/agiantin/cacheData/spring"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
/>
<cache name="NdgsList"
maxElementsInMemory="1"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="600"
timeToLiveSeconds="600"
/>
</ehcache>
set diskPersistent=true on the NdgsList cache.
-
Thank you for your reply.
It doesn't seems to be a problem of disk writing, the cache is never invoked, every time method findNdgsFromSettore(...) is called it's also executed and data are never loaded from cache, so I suppose they have never been put into cache...
-