org.apache.ignite.Ignite.transactions()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(8.3k)|赞(0)|评价(0)|浏览(203)

本文整理了Java中org.apache.ignite.Ignite.transactions()方法的一些代码示例,展示了Ignite.transactions()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Ignite.transactions()方法的具体详情如下:
包路径:org.apache.ignite.Ignite
类名称:Ignite
方法名:transactions

Ignite.transactions介绍

[英]Gets grid transactions facade.
[中]获取网格事务外观。

代码示例

代码示例来源:origin: apache/ignite

/**
 * Execute closure inside cache transaction.
 *
 * @param cache Cache.
 * @param concurrency Concurrency.
 * @param isolation Isolation.
 * @param clo Closure.
 * @throws IgniteCheckedException If failed.
 */
public static <K, V> void inTx(Ignite ignite, IgniteCache<K, V> cache, TransactionConcurrency concurrency,
  TransactionIsolation isolation, IgniteInClosureX<IgniteCache<K ,V>> clo) throws IgniteCheckedException {
  try (Transaction tx = ignite.transactions().txStart(concurrency, isolation)) {
    clo.applyx(cache);
    tx.commit();
  }
}

代码示例来源:origin: apache/ignite

@Override public Void call() throws Exception {
    Transaction tx = ignite0.transactions().txStart(PESSIMISTIC, REPEATABLE_READ);
    ignite0.cache(DEFAULT_CACHE_NAME).get(key);
    return null;
  }
}, "lock-thread1");

代码示例来源:origin: apache/ignite

@Override public Void call() throws Exception {
    try (Transaction tx = ignite.transactions().txStart(OPTIMISTIC, SERIALIZABLE)) {
      if (entry)
        readCache.get(readKey);
      else
        readCache.getEntry(readKey);
      barrier.await();
      writeCache.put(putKey.incrementAndGet(), 0);
      tx.commit();
    }
    return null;
  }
}, THREADS, "test-thread");

代码示例来源:origin: apache/ignite

@Override
  public void applyx() throws IgniteCheckedException {
    try (Transaction tx = ignite.transactions().txStart(TransactionConcurrency.PESSIMISTIC, isolation, timeout, keys.size())) {
      List<Integer> keys0 = getAndFlip(reverse) ? keys : keysReversed;
      for (int i = 0; i < keys0.size(); i++) {
        Integer key = keys0.get(i);
        if(oneOp)
          cache.getAndPut(key, new Account(key, (key + 1) * 100));
        else
          cache.put(key, new Account(cache.get(key).id, (key + 1) * 100));
        if (i == 0)
          barrier.await(timeout >> 1, TimeUnit.MILLISECONDS);
      }
      tx.commit();
    } catch (Exception e) {
      ex.compareAndSet(null, e);
    }
  }
}, 2, "tx");

代码示例来源:origin: apache/ignite

/**
 * @param node Node.
 * @param cache Cache.
 */
@SuppressWarnings("unchecked")
private void cacheOperations(Ignite node, IgniteCache<Integer, Integer> cache) {
  ThreadLocalRandom rnd = ThreadLocalRandom.current();
  Integer key = rnd.nextInt(1000);
  cache.put(key, key);
  cache.get(key);
  if (cache.getConfiguration(CacheConfiguration.class).getAtomicityMode() != TRANSACTIONAL_SNAPSHOT) {
    try (Transaction tx = node.transactions().txStart(OPTIMISTIC, REPEATABLE_READ)) {
      cache.put(key, key);
      tx.commit();
    }
  }
  try (Transaction tx = node.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
    cache.put(key, key);
    tx.commit();
  }
}

代码示例来源:origin: apache/ignite

/**
 * @param cache Cache.
 * @param key Key.
 * @param concurrency Transaction concurrency.
 * @param isolation Transaction isolation.
 * @throws Exception If failed.
 */
private void checkReadThroughGetAndInvoke(IgniteCache<Object, Object> cache,
  Object key,
  TransactionConcurrency concurrency,
  TransactionIsolation isolation) throws Exception {
  putDataInStore(Collections.singletonMap(key, key), cache.getName());
  try (Transaction tx = cache.unwrap(Ignite.class).transactions().txStart(concurrency, isolation)) {
    cache.get(key);
    Object ret = cache.invoke(key, new TestEntryProcessor());
    assertEquals(key, ret);
    tx.commit();
  }
  checkValue(cache.getName(), key, (Integer)key + 1);
}

代码示例来源:origin: apache/ignite

@Override public void run(IgniteCache<String, Integer> cache) throws IgniteCheckedException {
    Map.Entry<String, Integer> e = F.first(pairs(1).entrySet());
    assert e != null;
    String key = e.getKey();
    Integer val = e.getValue();
    try (Transaction tx = cache.unwrap(Ignite.class).transactions().txStart()) {
      assert cache.getAndPutAsync(key, val).get() == null;
      assert cache.containsKey(key);
      assert val.equals(cache.getAsync(key).get());
      assert val.equals(cache.getAndRemoveAsync(key).get());
      assert !cache.containsKey(key);
      tx.commit();
    }
    assert !cache.containsKey(key);
  }
}, F.t(EVT_CACHE_OBJECT_REMOVED, gridCnt));

代码示例来源:origin: apache/ignite

@Override public Void call() throws Exception {
    IgniteCache<Object, Object> cache = node1.cache(DEFAULT_CACHE_NAME);
    try (Transaction tx = node1.transactions().txStart()) {
      Integer key = keys.get(idx.getAndIncrement());
      cache.getAndPut(key, "new-" + key);
      tx.commit();
    }
    return null;
  }
}, txCnt, "tx");

代码示例来源:origin: apache/ignite

@Override public Object call() throws Exception {
    info(">>>> Starting grid.");
    Ignite g4 = startGrid(4);
    started.set(true);
    IgniteCache<Object, Object> c = g4.cache(DEFAULT_CACHE_NAME);
    info(">>>> Checking tx in new grid.");
    try (Transaction tx = g4.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
      assertEquals(2, c.get("a"));
      assertEquals(4, c.get("b"));
      assertEquals(6, c.get("c"));
    }
    return null;
  }
}, 1);

代码示例来源:origin: apache/ignite

/**
 * @param txMode Transaction concurrency mode.
 * @return Transaction.
 */
@Nullable private Transaction startTx(@Nullable TransactionConcurrency txMode) {
  return txMode == null ? null : ignite(0).transactions().txStart(txMode, REPEATABLE_READ);
}

代码示例来源:origin: apache/ignite

@Override public void run(IgniteCache<String, Integer> cache) throws IgniteCheckedException {
    Map.Entry<String, Integer> e = F.first(pairs(1).entrySet());
    assert e != null;
    String key = e.getKey();
    Integer val = e.getValue();
    try (Transaction tx = cache.unwrap(Ignite.class).transactions().txStart()) {
      assert cache.getAndPut(key, val) == null;
      assert cache.containsKey(key);
      assert val.equals(cache.get(key));
      assert val.equals(cache.getAndRemove(key));
      assert !cache.containsKey(key);
      tx.commit();
    }
    assert !cache.containsKey(key);
  }
}, F.t(EVT_CACHE_OBJECT_REMOVED, gridCnt));

代码示例来源:origin: apache/ignite

@Override public void run() {
    Ignite node = ignite(0);
    IgniteCache<Integer, Integer> cache = node.cache(DEFAULT_CACHE_NAME);
    ThreadLocalRandom rnd = ThreadLocalRandom.current();
    while (!stop.get()) {
      try (Transaction tx = node.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
        for (int i = 0; i < KEYS; i++) {
          if (rnd.nextBoolean())
            cache.get(i);
        }
      }
    }
  }
}, 3, "tx-thread");

代码示例来源:origin: apache/ignite

/**
 * @param txMode Transaction concurrency mode.
 * @return Transaction.
 */
@Nullable private Transaction startTx(@Nullable TransactionConcurrency txMode) {
  return txMode == null ? null : ignite(0).transactions().txStart(txMode, REPEATABLE_READ);
}

代码示例来源:origin: apache/ignite

@Override public void run(IgniteCache<String, Integer> cache) throws IgniteCheckedException {
    Map.Entry<String, Integer> e = F.first(pairs(1).entrySet());
    assert e != null;
    String key = e.getKey();
    Integer val = e.getValue();
    try (Transaction tx = cache.unwrap(Ignite.class).transactions().txStart()) {
      assert cache.getAndPut(key, val) == null;
      assert cache.containsKey(key);
      assert val.equals(cache.get(key));
      assert val.equals(cache.getAndRemove(key));
      assert !cache.containsKey(key);
      cache.put(key, val);
      assert cache.containsKey(key);
      tx.commit();
    }
    assert cache.containsKey(key);
  }
}, F.t(EVT_CACHE_OBJECT_PUT, gridCnt));

代码示例来源:origin: apache/ignite

@Override public void run() {
    U.awaitQuiet(l);
    try (final Transaction tx = ignite.transactions().txStart()) {
      cache.get(0);
      tx.commit();
    }
  }
});

代码示例来源:origin: apache/ignite

@Override public Object call() throws Exception {
    try (Transaction tx = ignite(0).transactions().txStart()) {
      jcache(0).lockAll(Arrays.asList("key1", "key2")).lock();
    }
    return null;
  }
},

代码示例来源:origin: apache/ignite

@Override public void run() {
    try {
      try (Transaction ignored = g0.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
        g0.cache(DEFAULT_CACHE_NAME).get(1);
        latch.countDown();
        Thread.sleep(500);
        info("Before stop.");
        G.stop(getTestIgniteInstanceName(1), true);
      }
    }
    catch (Exception e) {
      error("Error.", e);
    }
  }
});

代码示例来源:origin: apache/ignite

@Override public Object call() throws Exception {
    try (Transaction tx = ignite(0).transactions().txStart()) {
      jcache(0).lock("key").lock();
    }
    return null;
  }
},

代码示例来源:origin: apache/ignite

@Override public Object call() throws Exception {
    IgniteCache<Integer, Integer> cache = ignite1.cache(DEFAULT_CACHE_NAME);
    IgniteTransactions txs = ignite1.transactions();
    int val = idx.getAndIncrement();
    for (int i = 0; i < 100; i++) {
      try (Transaction tx = txs.txStart(optimistic ? OPTIMISTIC : PESSIMISTIC, REPEATABLE_READ)) {
        cache.get(key);
        cache.put(key, val);
        tx.commit();
      }
    }
    return null;
  }
}, 5, "put-thread");

代码示例来源:origin: apache/ignite

/**
 * @param concurrency Concurrency mode.
 * @param isolation Isolation mode.
 * @return Transaction.
 */
private Transaction startTx(TransactionConcurrency concurrency, TransactionIsolation isolation) {
  IgniteTransactions txs = ignite(0).transactions();
  if (concurrency == null)
    return txs.txStart();
  return txs.txStart(concurrency, isolation);
}

相关文章