1、redis的事务是通过MULTI、EXEC、DISCARD、WATCH、UNWATCH命令来实现的
原伪代码如下:
@Test public void test1() { final String REDIS_ROCK = "redis_lock"; String value = UUID.randomUUID().toString()+Thread.currentThread().getId(); try{ //TODO } finally { if (stringRedisTemplate.opsForValue().get(REDIS_ROCK).equalsIgnoreCase(value)) { stringRedisTemplate.delete(REDIS_ROCK); } } }
使用事务改造后的伪代码如下:
@Test public void test2() { final String REDIS_ROCK = "redis_lock"; String value = UUID.randomUUID().toString()+Thread.currentThread().getId(); try{ //TODO } finally { while (true) { stringRedisTemplate.watch(REDIS_ROCK); if (stringRedisTemplate.opsForValue().get(REDIS_ROCK).equalsIgnoreCase(value)) { stringRedisTemplate.setEnableTransactionSupport(true); stringRedisTemplate.multi(); stringRedisTemplate.delete(REDIS_ROCK); List
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)