spring data redis watch事务不执行问题

Posted Zshun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring data redis watch事务不执行问题相关的知识,希望对你有一定的参考价值。

 1 package com.devpg.redis;
 2 
 3 import org.junit.After;
 4 import org.junit.Assert;
 5 import org.junit.Test;
 6 import org.junit.runner.RunWith;
 7 import org.slf4j.Logger;
 8 import org.slf4j.LoggerFactory;
 9 import org.springframework.beans.factory.annotation.Autowired;
10 import org.springframework.dao.DataAccessException;
11 import org.springframework.data.redis.core.RedisOperations;
12 import org.springframework.data.redis.core.RedisTemplate;
13 import org.springframework.data.redis.core.SessionCallback;
14 import org.springframework.test.context.ContextConfiguration;
15 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
16 
17 @RunWith(SpringJUnit4ClassRunner.class)
18 @ContextConfiguration(locations = { "/testContext.xml" })
19 public class TransactionTest {
20 
21     Logger logger = LoggerFactory.getLogger(TransactionTest.class);
22 
23     @Autowired
24     RedisTemplate<String, Integer> template;
25 
26     private final String key = "tx-key";
27 
28     @After
29     public void deleteCounter() {
30         template.delete(key);
31     }
32     
33     @Test
34     public void useOptimisticLocking() {
35         final int valueSetInBetween = 23;
36         final int valueSetWithinSession = 42;
37 
38         /*
39          * By default each template method call creates a new connection - so
40          * WATCH, MUTLI, EXEC, UNWATCH won‘t work because of the missing
41          * context. To make use of transaction support use SessionCallback which
42          * reuses the underlying connection.
43          */
44         template.execute(new SessionCallback<Void>() {
45 
46             @Override
47             public Void execute(RedisOperations operations)
48                     throws DataAccessException {
49                 operations.watch(key);
50 
51                 setKeyByOtherBySession(valueSetInBetween);
52 
53                 operations.multi();
54                 operations.boundValueOps(key).set(valueSetWithinSession);
55                 operations.exec();
56 
57                 return null;
58             }
59         });
60 
61         int value = template.boundValueOps(key).get().intValue();
62         Assert.assertEquals(valueSetInBetween, value);
63     }
64 
65     private final void setKeyByOtherBySession(int value) {
66         template.boundValueOps(key).set(value);
67     }
68 }

关键时刻还是得找谷歌:https://github.com/devpg/spring-data-redis-example/blob/master/src/test/java/com/devpg/redis/TransactionTest.java

主要是这一句

/*
         * By default each template method call creates a new connection - so
         * WATCH, MUTLI, EXEC, UNWATCH won‘t work because of the missing
         * context. To make use of transaction support use SessionCallback which
         * reuses the underlying connection.
         */

 

以上是关于spring data redis watch事务不执行问题的主要内容,如果未能解决你的问题,请参考以下文章

如何以干净的方式在 Spring Data Redis 中实现事务?

Redis的事务和watch

Redis的事务和watch

redis 事务 和 watch

redis watch 加 事务实现秒杀

Redis 小白指南- 事务Watch 命令过期消息通知管道优化内存空间