PDA

View Full Version : RedisTemplate multi() and exec()



yukatan
Sep 13th, 2011, 06:50 AM
Hi,

I have a problem with the RedisTemplate class when I try to use it in a multi-thread envirotment. In the Spring Data Redis documentation I can read that the class is thread-safe, but when I use it, the multi() and Exec() methods look like they are interrupting each other.

Sometimes when I call the exec() method I get a jedis exception telling me that "I called an exec() method without call before multi()", but the multi() method is called just before. Is it posible to use the multi() and exec() this way??? maybe multi() and exec() are not thread-safe??

I tried an example with more than 100 threads running at the same time calling multi and exec() and setting values on redis in random time and I always get the same exception. If I remove the multi() and exec() all the threads end perfect and all the keys are created on redis without problems.

Thanks a lot.

yukatan
Sep 13th, 2011, 10:52 AM
The solution is to execute the code in a SessionCallback Object




template.execute(new SessionCallback() {

@Override
public Object execute(RedisOperations operations)
throws DataAccessException {

operations.opsForValue().set("Monkey", "Mandril");

}
})



documentation (http://static.springsource.org/spring-data/data-redis/docs/1.0.0.M4/api/)


Thanks.