Пример #1
0
  /** {@inheritDoc} */
  @Override
  protected long processInStreamOutLong(int type, BinaryRawReaderEx reader)
      throws IgniteCheckedException {
    long txId = reader.readLong();

    final Transaction asyncTx = (Transaction) tx(txId).withAsync();

    switch (type) {
      case OP_COMMIT_ASYNC:
        asyncTx.commit();

        break;

      case OP_ROLLBACK_ASYNC:
        asyncTx.rollback();

        break;

      default:
        return super.processInStreamOutLong(type, reader);
    }

    // Future result is the tx itself, we do not want to return it to the platform.
    IgniteFuture fut =
        asyncTx
            .future()
            .chain(
                new C1<IgniteFuture, Object>() {
                  private static final long serialVersionUID = 0L;

                  @Override
                  public Object apply(IgniteFuture fut) {
                    return null;
                  }
                });

    readAndListenFuture(reader, fut);

    return TRUE;
  }
  /** @throws IgniteCheckedException If test fails. */
  public void testOptimisticTransaction() throws Exception {
    CountDownLatch latch = new CountDownLatch(9);

    IgnitePredicate<Event> lsnr = new CacheEventListener(latch);

    try {
      IgniteCache<String, String> cache1 = ignite1.cache(null);
      IgniteCache<String, String> cache2 = ignite2.cache(null);
      IgniteCache<String, String> cache3 = ignite3.cache(null);

      ignite1.events().localListen(lsnr, EVT_CACHE_OBJECT_PUT, EVT_CACHE_OBJECT_REMOVED);
      ignite2.events().localListen(lsnr, EVT_CACHE_OBJECT_PUT, EVT_CACHE_OBJECT_REMOVED);
      ignite3.events().localListen(lsnr, EVT_CACHE_OBJECT_PUT, EVT_CACHE_OBJECT_REMOVED);

      Transaction tx = ignite1.transactions().txStart(OPTIMISTIC, READ_COMMITTED, 0, 0);

      try {
        cache1.put("tx1", "val1");
        cache1.put("tx2", "val2");
        cache1.put("tx3", "val3");

        assert cache2.get("tx1") == null;
        assert cache2.get("tx2") == null;
        assert cache2.get("tx3") == null;

        assert cache3.get("tx1") == null;
        assert cache3.get("tx2") == null;
        assert cache3.get("tx3") == null;

        tx.commit();
      } catch (CacheException e) {
        tx.rollback();

        throw e;
      }

      assert latch.await(5, SECONDS);

      String b1 = cache2.get("tx1");
      String b2 = cache2.get("tx2");
      String b3 = cache2.get("tx3");

      String c1 = cache3.get("tx1");
      String c2 = cache3.get("tx2");
      String c3 = cache3.get("tx3");

      assert b1 != null : "Invalid value: " + b1;
      assert b2 != null : "Invalid value: " + b2;
      assert b3 != null : "Invalid value: " + b3;

      assert c1 != null : "Invalid value: " + c1;
      assert c2 != null : "Invalid value: " + c2;
      assert c3 != null : "Invalid value: " + c3;

      assert "val1".equals(b1);
      assert "val2".equals(b2);
      assert "val3".equals(b3);

      assert "val1".equals(c1);
      assert "val2".equals(c2);
      assert "val3".equals(c3);
    } finally {
      ignite1.events().stopLocalListen(lsnr);
      ignite2.events().stopLocalListen(lsnr);
      ignite3.events().stopLocalListen(lsnr);
    }
  }