SpringBoot整合Redis事务

package com.learn.service;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class RedisService {

	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	
	public void setString(String key, Object object) {
		// 开启事务权限
		stringRedisTemplate.setEnableTransactionSupport(true);
		try {
			// 开启事务 begin
			stringRedisTemplate.multi();
			String value = (String) object;
			stringRedisTemplate.opsForValue().set(key, value);
			System.out.println("存入完毕,马上开始提交redis事务");
			// 提交事务
			stringRedisTemplate.exec();
		} catch (Exception e) {
			// 需要回滚事务
			stringRedisTemplate.discard();
		}
	}

}

 

你可能感兴趣的:(SpringBoot整合Redis事务)