spring data redis watch事務不執行問題

來源:互聯網
上載者:User

標籤: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     @Autowired24     RedisTemplate<String, Integer> template;25 26     private final String key = "tx-key";27 28     @After29     public void deleteCounter() {30         template.delete(key);31     }32     33     @Test34     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 - so40          * WATCH, MUTLI, EXEC, UNWATCH won‘t work because of the missing41          * context. To make use of transaction support use SessionCallback which42          * reuses the underlying connection.43          */44         template.execute(new SessionCallback<Void>() {45 46             @Override47             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 }

關鍵時刻還是得找Google: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事務不執行問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.