Results 1 to 2 of 2

Thread: RedisTemplate multi() and exec()

  1. #1
    Join Date
    Sep 2011
    Posts
    4

    Default 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. #2
    Join Date
    Sep 2011
    Posts
    4

    Default [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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •