码迷,mamicode.com
首页 > 编程语言 > 详细

spring data redis watch事务不执行问题

时间:2017-06-23 00:38:32      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:XML   clock   oid   access   logs   counter   lob   ram   template   

 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事务不执行问题

标签:XML   clock   oid   access   logs   counter   lob   ram   template   

原文地址:http://www.cnblogs.com/codespirit/p/7067848.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!