RedisTemplate 事务处理方法 watch multi exec 的使用

1. 前言

在分布式环境下,对redis中的同一个变量进行操作,如果不注意,很容易就出现数据错乱的问题,简单的例子有10个线程,
同时对某个变量(0)进行+1的操作,正常情况下,变量的最终值应该是10,但如果处理不当,最终值有可能不是10

2. 有问题的代码

    String key = "multThreadTest";

	redisTemplate.delete(key);

	Runnable runable = new Runnable() {
		@Override
		public void run() {
			int count = 0;
			String value = (String) redisTemplate.opsForValue().get(key);
			if (!StringUtils.isEmpty(value)) {
				count = Integer.parseInt(value);
			}
			count = count + 1;
			redisTemplate.opsForValue().set(key, String.valueOf(count));
		}
	};

	List<Thread> threads = new ArrayList<>();
	for (int i = 0; i < 10; i++) {
		Thread thread = new Thread(runable, "thread-" + (i + 1));
		thread.start();
		threads.add(thread);
	}

	for (Thread thread : threads) {
		thread.join();
	}

	String value = redisTemplate.opsForValue().get(key);
	System.out.println("value:" + value);
可以看到,上面的代码,每次执行得到的结果都不一样

2. 正确处理的代码

	String key = "multThreadTest";
	
	redisTemplate.delete(key);
	
	Runnable runable = new Runnable() {
		@Override
		public void run() {
	
			redisTemplate.execute(new SessionCallback<Object>() {
	
				@Override
				@SuppressWarnings({ "unchecked", "rawtypes" })
				public Object execute(RedisOperations operations) throws DataAccessException {
					List<Object> result = null;
					do {
						int count = 0;
						operations.watch(key);  // watch某个key,当该key被其它客户端改变时,则会中断当前的操作
						String value = (String) operations.opsForValue().get(key);
						if (!StringUtils.isEmpty(value)) {
							count = Integer.parseInt(value);
						}
						count = count + 1;
						operations.multi(); //开始事务
						operations.opsForValue().set(key, String.valueOf(count));
						try {
							result = operations.exec(); //提交事务
						} catch (Exception e) { //如果key被改变,提交事务时这里会报异常
	
						}
					} while (result == null); //如果失败则重试
					return null;
				}
	
			});
	
		}
	};
	List<Thread> threads = new ArrayList<>();
	for (int i = 0; i < 10; i++) {
		Thread thread = new Thread(runable, "thread-" + (i + 1));
		thread.start();
		threads.add(thread);
	}
	
	for (Thread thread : threads) {
		thread.join();
	}
	
	String value = redisTemplate.opsForValue().get(key);
	System.out.println(value);
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值