2017-08-06 8 views
0

こんにちは私は春にredisを使用し、@Cacheableを使用して実装しています。以下 は私の春とRedisのバージョンである:マルチでJedisを使用することはできません。代わりにJedisTransactionを使用してください

のRedis: 春-データRedisの1.5.0.RELEASE jedis 2.6.1

春:以下4.1.1.RELEASE

は私のRedisのです構成。

import java.util.Arrays; 

import org.apache.log4j.Logger; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.cache.CacheManager; 
import org.springframework.cache.annotation.CachingConfigurerSupport; 
import org.springframework.cache.annotation.EnableCaching; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.data.redis.cache.RedisCacheManager; 
import org.springframework.data.redis.connection.RedisConnectionFactory; 
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; 
import org.springframework.data.redis.core.RedisTemplate; 
import org.springframework.data.redis.serializer.RedisSerializer; 
import org.springframework.data.redis.serializer.StringRedisSerializer; 

import redis.clients.jedis.JedisPoolConfig; 

@Configuration 
@EnableCaching 
public class CacheConfig extends CachingConfigurerSupport { 

    private static final Logger log = Logger.getLogger(CacheConfig.class); 

    @Autowired 
    RedisConfig redisConfig; 

    @Bean 
    public JedisConnectionFactory redisConnectionFactory() { 
     JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory(); 

     JedisPoolConfig config = new JedisPoolConfig(); 
     config.setMaxTotal(1000); 
     config.setMaxIdle(10); 
     config.setMinIdle(1); 
     config.setMaxWaitMillis(30000); 

     // Defaults 
     redisConnectionFactory.setHostName(redisConfig.getRedisHost()); 
     redisConnectionFactory.setPort(redisConfig.getRedisPort()); 
     redisConnectionFactory.setDatabase(redisConfig.getRedisDatabase()); 
     redisConnectionFactory.setPoolConfig(config); 
     redisConnectionFactory.setUsePool(true); 

     return redisConnectionFactory; 
    } 

    @Bean 
    public StringRedisSerializer redisSerializer() { 
     StringRedisSerializer redisSerializer = new StringRedisSerializer(); 

     return redisSerializer; 
    } 

    @Bean 
    public RedisTemplate<String, String> redisTemplate(JedisConnectionFactory cf, RedisSerializer sl) { 
     RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>(); 
     redisTemplate.setConnectionFactory(cf); 
     redisTemplate.setKeySerializer(sl); 
     redisTemplate.setHashKeySerializer(sl); 
     redisTemplate.setEnableTransactionSupport(true); 

     return redisTemplate; 
    } 

    @Bean 
    public CacheManager cacheManager(RedisTemplate redisTemplate) { 
     RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate); 

     // Number of seconds before expiration. Defaults to unlimited (0) 
     cacheManager.setDefaultExpiration(3600); 
     cacheManager.setCacheNames(Arrays.asList("my-cache")); 
     cacheManager.setTransactionAware(true); 
     cacheManager.setLoadRemoteCachesOnStartup(true); 
     cacheManager.setUsePrefix(true); 
     return cacheManager; 
    } 

} 

しかし、私は、コードを実行する上で次の例外を取得しています:

org.springframework.dao.InvalidDataAccessApiUsageException: Cannot use Jedis when in Multi. Please use JedisTransaction instead.; nested exception is redis.clients.jedis.exceptions.JedisDataException: Cannot use Jedis when in Multi. Please use JedisTransaction instead. 
    at org.springframework.data.redis.connection.jedis.JedisExceptionConverter.convert(JedisExceptionConverter.java:44) 
    at org.springframework.data.redis.connection.jedis.JedisExceptionConverter.convert(JedisExceptionConverter.java:36) 
    at org.springframework.data.redis.PassThroughExceptionTranslationStrategy.translate(PassThroughExceptionTranslationStrategy.java:37) 
    at org.springframework.data.redis.FallbackExceptionTranslationStrategy.translate(FallbackExceptionTranslationStrategy.java:37) 
    at org.springframework.data.redis.connection.jedis.JedisConnection.convertJedisAccessException(JedisConnection.java:196) 
    at org.springframework.data.redis.connection.jedis.JedisConnection.close(JedisConnection.java:253) 
    at org.springframework.data.redis.core.RedisConnectionUtils.unbindConnection(RedisConnectionUtils.java:230) 
    at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:203) 
    at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:153) 
    at org.springframework.data.redis.cache.RedisCache.put(RedisCache.java:140) 
    at org.springframework.data.redis.cache.RedisCache.put(RedisCache.java:125) 
    at org.springframework.cache.transaction.TransactionAwareCacheDecorator.put(TransactionAwareCacheDecorator.java:85) 
    at org.springframework.cache.interceptor.AbstractCacheInvoker.doPut(AbstractCacheInvoker.java:82) 
    at org.springframework.cache.interceptor.CacheAspectSupport$CachePutRequest.apply(CacheAspectSupport.java:651) 
    at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:358) 
    at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:299) 
    at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:61) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) 

私が間違って何をやっているなら、私を提案してください。

EDIT:サービスクラスの実装。

import java.util.List; 

import org.apache.log4j.Logger; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.cache.annotation.Cacheable; 
import org.springframework.stereotype.Service; 
import org.springframework.transaction.annotation.Transactional; 

@Service 
public class LocationMasterService implements ILocationMasterService { 
    private final Logger logger = Logger.getLogger(getClass()); 

    @Autowired 
    ILocationMasterDao locationMasterDao; 

    @Override 
    @Cacheable(value = "my-cache", key = "#root.methodName") 
    public List<LocationMasterResponse> getLocation() { 
     logger.debug("Inside StoreMasterService createStore action "); 
     List<LocationMasterResponse> locationList = null; 

     List<LocationMasterEntity> locationMasterEntityList = null; 
     locationMasterEntityList = locationMasterDao.getLocation(); 

     locationList = MapperUtil.map(locationMasterEntityList, LocationMasterResponse.class); 


     return locationList; 
    } 

} 

答えて

1

私は、同じ例外を取得し、デバッグする際の問題は、私はキャッシュしていた値がserialisableではなく、そこに失敗したことがあることが分かりました。私の場合、例外とスタックトレースは間違っていました。キャッシュしているオブジェクトが直列化可能かどうかを確認します。

+1

これで問題が解決しました。しかし、別のコードでは同じ問題が発生していますが、オブジェクト直列化のためにこの時間をチェックしました。 – nikhil1265

関連する問題