redis redisson 事务示例
redisson可对一些对象进行事务操作,这些对象包括RBucket、Rbuckets,RSet、RSetCache,RMap、RMapCache、RLocalCachedMap
*******************
相关接口
RTransaction
public interface RTransaction {
********
RBucket、RBuckets
<V> RBucket<V> getBucket(String var1);
<V> RBucket<V> getBucket(String var1, Codec var2);
RBuckets getBuckets();
RBuckets getBuckets(Codec var1);
********
RSet、RSetCache
<V> RSet<V> getSet(String var1);
<V> RSet<V> getSet(String var1, Codec var2);
<V> RSetCache<V> getSetCache(String var1);
<V> RSetCache<V> getSetCache(String var1, Codec var2);
********
RMap、RMapCache、RLocalCachedMap
<K, V> RMap<K, V> getMap(String var1);
<K, V> RMap<K, V> getMap(String var1, Codec var2);
<K, V> RMapCache<K, V> getMapCache(String var1);
<K, V> RMapCache<K, V> getMapCache(String var1, Codec var2);
<K, V> RLocalCachedMap<K, V> getLocalCachedMap(RLocalCachedMap<K, V> var1);
********
其他方法
void commit();
RFuture<Void> commitAsync(); //提交事务
void rollback();
RFuture<Void> rollbackAsync();//回滚事务
}
说明:每次创建的transaction只能进行一次提交或者回滚,不能重复使用;如果需要多个事务操作,需创建多个事务
TransactionOptions
public final class TransactionOptions {
private long responseTimeout = 3000L; //返回超时时间
private int retryAttempts = 3; //重试次数
private long retryInterval = 1500L; //重试间隔
private long syncTimeout = 5000L; //从节点同步超时时间
private long timeout = 5000L; //超时时间
********
构造方法
private TransactionOptions() {
}
********
普通方法
public static TransactionOptions defaults() { //返回TransactionOtiopns对象实例
return new TransactionOptions();
}
public long getResponseTimeout() { //返回超时时间
public TransactionOptions responseTimeout(long timeout, TimeUnit unit) {
//设置超时时间
public int getRetryAttempts() { //返回重试次数
public TransactionOptions retryAttempts(int retryAttempts) {
//设置重试次数
public long getRetryInterval() { //返回重试间隔
public TransactionOptions retryInterval(long retryInterval, TimeUnit retryIntervalUnit) {
//设置重试间隔
public long getSyncTimeout() { //返回从节点同步超时时间
public TransactionOptions syncSlavesTimeout(long syncTimeout, TimeUnit syncUnit) {
//设置从节点同步超时时间
public long getTimeout() { //返回超时时间
public TransactionOptions timeout(long timeout, TimeUnit timeoutUnit) {
//设置超时时间
}
*******************
示例
public class MyTest9 {
public static void main(String[] args){
Config config=new Config();
config.useSingleServer().setAddress("redis://192.168.57.120:6379").setPassword("123456");
RedissonClient client= Redisson.create(config);
RTransaction transaction=client.createTransaction(TransactionOptions.defaults());
RSet<String> set=transaction.getSet("transaction_set");
try{
for(int i=0;i<10;i++){
set.add("瓜田李下 "+i);
}
transaction.commit();
}catch (Exception e){
transaction.rollback();
e.printStackTrace();
}
System.out.println("set的大小为:"+set.size());
RTransaction transaction2=client.createTransaction(TransactionOptions.defaults());
RMap<Integer,String> map=transaction2.getMap("transaction_map");
try{
for (int i=0;i<10;i++){
map.put(i,"瓜田李下 "+i);
if (i==2){
throw new RuntimeException("出错了");
}
}
transaction.commit();
}catch (Exception e){
e.printStackTrace();
}
System.out.println("map的大小为:"+map.size());
}
}
************
控制台输出
set的大小为:10
map的大小为:0
说明:set的大小为10表明事务transaction提交,map的大小为0表明事务回滚