Announcement Announcement Module
Collapse
No announcement yet.
RedisTemplate multi() and exec() Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • RedisTemplate multi() and exec()

    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.

  • #2
    [solved]

    The solution is to execute the code in a SessionCallback Object

    Code:
    template.execute(new SessionCallback() {
    				
    				@Override
    				public Object execute(RedisOperations operations)
    						throws DataAccessException {
    					
    					operations.opsForValue().set("Monkey", "Mandril");
    						
    				}
    			})
    documentation


    Thanks.

    Comment

    Working...
    X