0

This error shows up after I upgraded spring-data-redis from 1.4.2 to 1.5.2.

My code is to implement a pessimistic lock,

redisTemplate.execute(new RedisCallback<Boolean>() {
                @Override
                public Boolean doInRedis(RedisConnection connection)
                        throws DataAccessException {
Boolean nx = connection.setNX(lockKey.getBytes(), identifier.getBytes());
return nx;
}};

Checked all fields not null. The setNx method always returns null. It should return boolean true or false.

Any help is appreciated.

Updates:

Above code snippet was nested in another SessionCallback Redis transaction. It used to work under spring-data-redis 1.4.2. To fix "ERR EXEC without MULTI" reported at ERR EXEC without MULTI - spring redis+jedis, I upgraded the lib to 1.5.2, and the problem suddenly showed up.

To fix it, I first tried to assign a different redisTemplate (prototype bean) for the nested callback method, but it didn't work. The working solution is to break up and flatten the single transaction into 4, "test if lock is needed", "acquire lock", "do the business", "release lock".

Community
  • 1
  • 1
Xinyu Liu
  • 1
  • 2

1 Answers1

0

It appears the issue was caused by nesting Redis transactions. Nesting SessionCallback even with distinct redisTemplate (different database connections) fails the nested transaction. The working solution is to flatten to 4 transactions, "test if lock is needed", "acquire lock", "do the business", "release lock".

Xinyu Liu
  • 1
  • 2