Java Code Examples for org.redisson.api.RTransaction#commit()

The following examples show how to use org.redisson.api.RTransaction#commit() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RedissonTransactionalSetTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testRemove() {
    RSet<String> s = redisson.getSet("test");
    s.add("1");
    s.add("3");
    
    RTransaction transaction = redisson.createTransaction(TransactionOptions.defaults());
    RSet<String> set = transaction.getSet("test");
    assertThat(set.contains("1")).isTrue();
    assertThat(set.remove("3")).isTrue();
    assertThat(set.remove("3")).isFalse();
    assertThat(set.remove("3")).isFalse();
    
    assertThat(s.contains("3")).isTrue();
    
    transaction.commit();
    
    assertThat(s.size()).isEqualTo(1);
    assertThat(s.contains("1")).isTrue();
    assertThat(s.contains("3")).isFalse();
}
 
Example 2
Source File: RedissonBaseTransactionalMapTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testReplace2() {
    RMap<String, String> m = getMap();
    m.put("1", "2");
    m.put("3", "4");
    
    RTransaction transaction = redisson.createTransaction(TransactionOptions.defaults());
    RMap<String, String> map = getTransactionalMap(transaction);
    assertThat(map.replace("3", "4", "10")).isTrue();
    assertThat(map.replace("1", "1", "3")).isFalse();
    assertThat(map.replace("3", "10", "11")).isTrue();
    
    assertThat(m.get("3")).isEqualTo("4");
    assertThat(m.get("1")).isEqualTo("2");
    assertThat(m.size()).isEqualTo(2);
    
    transaction.commit();
    
    assertThat(m.size()).isEqualTo(2);
    assertThat(m.get("3")).isEqualTo("11");
    assertThat(m.get("1")).isEqualTo("2");
}
 
Example 3
Source File: RedissonBaseTransactionalMapTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testPutRemove() {
    RMap<String, String> m = getMap();
    m.put("1", "2");
    m.put("3", "4");
    
    RTransaction transaction = redisson.createTransaction(TransactionOptions.defaults());
    RMap<String, String> map = getTransactionalMap(transaction);
    assertThat(map.get("1")).isEqualTo("2");
    assertThat(map.remove("3")).isEqualTo("4");
    assertThat(map.put("3", "5")).isNull();
    assertThat(map.get("3")).isEqualTo("5");
    
    assertThat(m.get("3")).isEqualTo("4");
    
    transaction.commit();
    
    assertThat(m.get("1")).isEqualTo("2");
    assertThat(m.get("3")).isEqualTo("5");
}
 
Example 4
Source File: RedissonBaseTransactionalMapTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test(expected = TransactionTimeoutException.class)
public void testParallel(){
    RMap<Integer, String> m = redisson.getMap("test");
    m.put(1, "test");
    
    RTransaction transaction1 = redisson.createTransaction(TransactionOptions.defaults().timeout(10, TimeUnit.SECONDS));
    RMap<Integer, String> map1 = transaction1.getMap("put_test");
    map1.remove(1);

    RTransaction transaction2 = redisson.createTransaction(TransactionOptions.defaults());
    RMap<Integer, String> map2 = transaction2.getMap("put_test");
    map2.put(1,"aryan");
    
    try {
        transaction2.commit();
        Assert.fail();
    } catch (TransactionTimeoutException e) {
        // skip
    }
    
    transaction1.commit();
    assertThat(m.size()).isZero();
}
 
Example 5
Source File: RedissonTransactionalLocalCachedMapTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testPut() throws InterruptedException {
    RLocalCachedMap<String, String> m1 = redisson.getLocalCachedMap("test", LocalCachedMapOptions.defaults());
    m1.put("1", "2");
    m1.put("3", "4");
    
    RLocalCachedMap<String, String> m2 = redisson.getLocalCachedMap("test", LocalCachedMapOptions.defaults());
    m2.get("1");
    m2.get("3");
    
    RTransaction transaction = redisson.createTransaction(TransactionOptions.defaults());
    RMap<String, String> map = transaction.getLocalCachedMap(m1);
    assertThat(map.put("3", "5")).isEqualTo("4");
    assertThat(map.get("3")).isEqualTo("5");
    
    assertThat(m1.get("3")).isEqualTo("4");
    assertThat(m2.get("3")).isEqualTo("4");
    
    transaction.commit();
    
    assertThat(m1.get("3")).isEqualTo("5");
    assertThat(m2.get("3")).isEqualTo("5");
}
 
Example 6
Source File: RedissonTransactionalBucketsTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testSet() {
    RBucket<String> b1 = redisson.getBucket("test1");
    b1.set("1");
    RBucket<String> b2 = redisson.getBucket("test2");
    b2.set("2");
    
    RTransaction transaction = redisson.createTransaction(TransactionOptions.defaults());
    RBuckets buckets = transaction.getBuckets();
    Map<String, Object> bbs = new LinkedHashMap<>();
    bbs.put("test1", "11");
    bbs.put("test2", "22");
    buckets.set(bbs);
    
    Map<String, Object> newBuckets = buckets.get("test1", "test2");
    assertThat(newBuckets).isEqualTo(bbs);
    
    transaction.commit();
    
    assertThat(redisson.getBuckets().get("test1", "test2")).isEqualTo(bbs);
    assertThat(redisson.getKeys().count()).isEqualTo(2);
}
 
Example 7
Source File: TransactionExamples.java    From redisson-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    // connects to 127.0.0.1:6379 by default
    RedissonClient redisson = Redisson.create();
    
    RBucket<String> b = redisson.getBucket("test");
    b.set("123");
    
    RTransaction transaction = redisson.createTransaction(TransactionOptions.defaults());
    RBucket<String> bucket = transaction.getBucket("test");
    bucket.set("234");
    
    RMap<String, String> map = transaction.getMap("myMap");
    map.put("1", "2");
    
    transaction.commit();
}
 
Example 8
Source File: RedissonBaseTransactionalMapTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testPutIfAbsent() {
    RMap<String, String> m = getMap();
    m.put("1", "2");
    m.put("3", "4");
    
    RTransaction transaction = redisson.createTransaction(TransactionOptions.defaults());
    RMap<String, String> map = getTransactionalMap(transaction);
    assertThat(map.putIfAbsent("3", "2")).isEqualTo("4");
    assertThat(map.putIfAbsent("5", "6")).isNull();
    assertThat(map.putIfAbsent("5", "7")).isEqualTo("6");
    
    assertThat(m.get("3")).isEqualTo("4");
    assertThat(m.size()).isEqualTo(2);
    
    transaction.commit();
    
    assertThat(m.get("1")).isEqualTo("2");
    assertThat(m.get("3")).isEqualTo("4");
    assertThat(m.get("5")).isEqualTo("6");
}
 
Example 9
Source File: RedissonBaseTransactionalMapTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testPutIfAbsentRemove() {
    RMap<String, String> m = getMap();
    m.put("1", "2");
    m.put("3", "4");
    
    RTransaction transaction = redisson.createTransaction(TransactionOptions.defaults());
    RMap<String, String> map = getTransactionalMap(transaction);
    assertThat(map.putIfAbsent("3", "2")).isEqualTo("4");
    assertThat(map.putIfAbsent("5", "6")).isNull();
    assertThat(map.putIfAbsent("5", "7")).isEqualTo("6");
    assertThat(map.remove("5")).isEqualTo("6");
    assertThat(map.putIfAbsent("5", "8")).isNull();
    
    assertThat(m.get("3")).isEqualTo("4");
    assertThat(m.size()).isEqualTo(2);
    
    transaction.commit();
    
    assertThat(m.get("1")).isEqualTo("2");
    assertThat(m.get("3")).isEqualTo("4");
    assertThat(m.get("5")).isEqualTo("8");
}
 
Example 10
Source File: RedissonBaseTransactionalMapTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testPut() {
    RMap<String, String> m = getMap();
    m.put("1", "2");
    m.put("3", "4");
    
    RTransaction transaction = redisson.createTransaction(TransactionOptions.defaults());
    RMap<String, String> map = getTransactionalMap(transaction);
    assertThat(map.put("3", "5")).isEqualTo("4");
    assertThat(map.get("3")).isEqualTo("5");
    
    assertThat(m.get("3")).isEqualTo("4");
    
    transaction.commit();
    
    assertThat(m.get("1")).isEqualTo("2");
    assertThat(m.get("3")).isEqualTo("5");
}
 
Example 11
Source File: RedissonTransactionalBucketTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testCompareAndSet() {
    RBucket<String> b = redisson.getBucket("test");
    b.set("123");
    
    RTransaction transaction = redisson.createTransaction(TransactionOptions.defaults());
    RBucket<String> bucket = transaction.getBucket("test");
    assertThat(bucket.compareAndSet("0", "434")).isFalse();
    assertThat(bucket.get()).isEqualTo("123");
    assertThat(bucket.compareAndSet("123", "232")).isTrue();
    assertThat(bucket.get()).isEqualTo("232");
    
    transaction.commit();
    
    assertThat(redisson.getKeys().count()).isEqualTo(1);
    assertThat(b.get()).isEqualTo("232");
}
 
Example 12
Source File: RedissonBaseTransactionalMapTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testRemove() {
    RMap<String, String> m = getMap();
    m.put("1", "2");
    m.put("3", "4");
    
    RTransaction transaction = redisson.createTransaction(TransactionOptions.defaults());
    RMap<String, String> map = getTransactionalMap(transaction);
    assertThat(map.get("1")).isEqualTo("2");
    assertThat(map.remove("3")).isEqualTo("4");
    assertThat(map.remove("3")).isNull();
    assertThat(map.remove("3")).isNull();
    
    assertThat(m.get("3")).isEqualTo("4");
    
    transaction.commit();
    
    assertThat(m.get("1")).isEqualTo("2");
    assertThat(m.get("3")).isNull();
}
 
Example 13
Source File: RedissonTransactionalSetCacheTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testRemoveAll() {
    RSetCache<String> s = redisson.getSetCache("test");
    s.add("1");
    s.add("3");
    
    RTransaction t = redisson.createTransaction(TransactionOptions.defaults());
    RSetCache<String> set = t.getSetCache("test");
    Set<String> putSet = new HashSet<String>();
    putSet.add("4");
    putSet.add("3");
    set.removeAll(putSet);
    assertThat(s).containsOnly("1", "3");
    assertThat(set).containsOnly("1");
    
    t.commit();
    
    assertThat(s).containsOnly("1");
}
 
Example 14
Source File: RedissonTransactionalBucketTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testSet() {
    RBucket<String> b = redisson.getBucket("test");
    b.set("123");
    
    RTransaction transaction = redisson.createTransaction(TransactionOptions.defaults());
    RBucket<String> bucket = transaction.getBucket("test");
    bucket.set("234");
    assertThat(bucket.get()).isEqualTo("234");
    
    transaction.commit();
    
    assertThat(redisson.getKeys().count()).isEqualTo(1);
    assertThat(b.get()).isEqualTo("234");
}
 
Example 15
Source File: RedissonTransactionalMapCacheTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testPutIfAbsentTTL() throws InterruptedException {
    RMapCache<Object, Object> m = redisson.getMapCache("test");
    m.put("1", "2");
    m.put("3", "4");
    
    RTransaction transaction = redisson.createTransaction(TransactionOptions.defaults());
    RMapCache<Object, Object> map = transaction.getMapCache("test");
    assertThat(map.putIfAbsent("3", "2", 1, TimeUnit.SECONDS)).isEqualTo("4");
    assertThat(map.putIfAbsent("5", "6", 3, TimeUnit.SECONDS)).isNull();
    assertThat(map.putIfAbsent("5", "7", 1, TimeUnit.SECONDS)).isEqualTo("6");
    
    assertThat(m.get("3")).isEqualTo("4");
    assertThat(m.size()).isEqualTo(2);
    
    transaction.commit();
    
    assertThat(m.get("1")).isEqualTo("2");
    assertThat(m.get("3")).isEqualTo("4");
    assertThat(m.get("5")).isEqualTo("6");
    
    Thread.sleep(1500);
    
    assertThat(m.get("3")).isEqualTo("4");
    assertThat(m.get("5")).isEqualTo("6");
    
    Thread.sleep(1500);
    
    assertThat(m.get("3")).isEqualTo("4");
    assertThat(m.get("5")).isNull();
}
 
Example 16
Source File: RedissonTransactionalBucketsTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testGet() {
    RBucket<String> b = redisson.getBucket("test");
    b.set("123");
    
    RTransaction transaction = redisson.createTransaction(TransactionOptions.defaults());
    RBuckets buckets = transaction.getBuckets();
    assertThat(buckets.get("test").get("test")).isEqualTo("123");
    
    transaction.commit();
    
    assertThat(redisson.getKeys().count()).isEqualTo(1);
    assertThat(b.get()).isEqualTo("123");
}
 
Example 17
Source File: RedissonTransactionalBucketsTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
    public void testTrySet() {
        redisson.getBucket("test1").set("1");
        redisson.getBucket("test2").set("2");
        
        RTransaction transaction = redisson.createTransaction(TransactionOptions.defaults());
        RBuckets buckets = transaction.getBuckets();
        Map<String, Object> bbs1 = new LinkedHashMap<>();
        bbs1.put("test1", "10");
        bbs1.put("test2", "20");
        assertThat(buckets.trySet(bbs1)).isFalse();
        assertThat(buckets.delete("test1", "test2")).isEqualTo(2);
        Map<String, Object> bbs2 = new LinkedHashMap<>();
        bbs2.put("test1", "11");
        bbs2.put("test2", "22");
        assertThat(buckets.trySet(bbs2)).isTrue();
        Map<String, Object> bbs3 = new LinkedHashMap<>();
        bbs3.put("test1", "13");
        bbs3.put("test2", "23");
        assertThat(buckets.trySet(bbs3)).isFalse();
        
        System.out.println("commit " + Thread.currentThread().getId());
        transaction.commit();
        redisson.getKeys().getKeys().forEach(x -> System.out.println(x));
        
//        assertThat(redisson.getBuckets().get("test1", "test2")).isEqualTo(bbs2);
        assertThat(redisson.getKeys().count()).isEqualTo(2);
    }
 
Example 18
Source File: RedissonBaseTransactionalMapTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testReplace() {
    RMap<String, String> m = getMap();
    m.put("1", "2");
    m.put("3", "4");
    
    RTransaction transaction = redisson.createTransaction(TransactionOptions.defaults());
    RMap<String, String> map = getTransactionalMap(transaction);
    assertThat(map.replace("3", "10")).isEqualTo("4");
    assertThat(map.replace("5", "0")).isNull();
    
    assertThat(m.get("3")).isEqualTo("4");
    assertThat(m.size()).isEqualTo(2);
    
    transaction.commit();
    
    assertThat(m.size()).isEqualTo(2);
    assertThat(m.get("3")).isEqualTo("10");
    assertThat(m.get("5")).isNull();
    
    RTransaction transaction2 = redisson.createTransaction(TransactionOptions.defaults());
    RMap<String, String> map2 = getTransactionalMap(transaction2);
    assertThat(map2.replace("3", "20")).isEqualTo("10");
    assertThat(map2.replace("3", "30")).isEqualTo("20");
    
    assertThat(m.get("3")).isEqualTo("10");
    assertThat(m.size()).isEqualTo(2);
    
    transaction2.commit();
    
    assertThat(m.get("3")).isEqualTo("30");
}
 
Example 19
Source File: RedissonTransactionalLocalCachedMapTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testPutRemove() {
    RLocalCachedMap<String, String> m1 = redisson.getLocalCachedMap("test", LocalCachedMapOptions.defaults());
    m1.put("1", "2");
    m1.put("3", "4");
    
    RLocalCachedMap<String, String> m2 = redisson.getLocalCachedMap("test", LocalCachedMapOptions.defaults());
    m2.get("1");
    m2.get("3");
    
    RTransaction transaction = redisson.createTransaction(TransactionOptions.defaults());
    RMap<String, String> map = transaction.getLocalCachedMap(m1);
    assertThat(map.get("1")).isEqualTo("2");
    assertThat(map.remove("3")).isEqualTo("4");
    assertThat(map.put("3", "5")).isNull();
    assertThat(map.get("3")).isEqualTo("5");
    
    assertThat(m1.get("3")).isEqualTo("4");
    assertThat(m2.get("3")).isEqualTo("4");
    
    transaction.commit();
    
    assertThat(m1.get("1")).isEqualTo("2");
    assertThat(m1.get("3")).isEqualTo("5");
    assertThat(m2.get("1")).isEqualTo("2");
    assertThat(m2.get("3")).isEqualTo("5");
}
 
Example 20
Source File: RedissonBaseTransactionalMapTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetAll() {
    RMap<String, String> m = getMap();
    m.put("1", "2");
    m.put("3", "4");
    
    RTransaction t = redisson.createTransaction(TransactionOptions.defaults());
    RMap<String, String> map = getTransactionalMap(t);
    assertThat(map.getAll(new HashSet<String>(Arrays.asList("1", "3"))).values()).containsOnly("2", "4");
    
    t.commit();
}