## spring data redis提供兩種方式
1.編程式《同步執行》
~~~
redisTemplate.execute(new SessionCallback<Object>() {
@Override
public Object execute(RedisOperations operations) throws DataAccessException {
operations.unwatch();// 如果其他事線程中分銷修改則回滾事務
operations.multi();// 開啟事務
// 無需try回滾事務,出現異常底層會自動回滾
operations.opsForValue().set("execute", "事務");
int i = 1 / 0;
return operations.exec();// 提交事務
}
});
~~~
2.spring聲明式事務
~~~
@Transactional
public Object index() {
template.setEnableTransactionSupport(true);// 設置支持spring聲明式事務
redisTemplate.boundValueOps("ABC").set("ACB");
int i=1/0;
Object abc = redisTemplate.boundValueOps("ABC").get();
return abc;
}
~~~