org.apache.tinkerpop.gremlin.structure.Transaction.createThreadedTx()方法的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(8.5k)|赞(0)|评价(0)|浏览(82)

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

Transaction.createThreadedTx介绍

[英]Creates a transaction that can be executed across multiple threads. The Graph returned from this method is not meant to represent some form of child transaction that can be committed from this object. A threaded transaction is a Graph instance that has a transaction context that enables multiple threads to collaborate on the same transaction. A standard transactional context tied to a Graphthat supports transactions will typically bind a transaction to a single thread via ThreadLocal.
[中]创建可以跨多个线程执行的事务。此方法返回的图形并不表示可以从此对象提交的某种形式的子事务。线程事务是具有事务上下文的图形实例,该事务上下文允许多个线程在同一事务上协作。绑定到支持事务的图形的标准事务上下文通常会通过ThreadLocal将事务绑定到单个线程。

代码示例

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

@Test
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES)
@FeatureRequirement(featureClass = Graph.Features.GraphFeatures.class, feature = Graph.Features.GraphFeatures.FEATURE_TRANSACTIONS)
@FeatureRequirement(featureClass = Graph.Features.GraphFeatures.class, feature = Graph.Features.GraphFeatures.FEATURE_THREADED_TRANSACTIONS)
public void shouldSupportMultipleThreadsOnTheSameTransaction() throws Exception {
  final int numberOfThreads = 10;
  final CountDownLatch latch = new CountDownLatch(numberOfThreads);
  final Graph threadedG = g.tx().createThreadedTx();
  for (int ix = 0; ix < numberOfThreads; ix++) {
    new Thread(() -> {
      threadedG.addVertex();
      latch.countDown();
    }).start();
  }
  latch.await(10000, TimeUnit.MILLISECONDS);
  // threaded transaction is not yet committed so g should not reflect any change
  assertVertexEdgeCounts(graph, 0, 0);
  threadedG.tx().commit();
  // there should be one vertex for each thread
  assertVertexEdgeCounts(graph, numberOfThreads, 0);
}

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

@Test
  @FeatureRequirement(featureClass = Graph.Features.GraphFeatures.class, feature = FEATURE_TRANSACTIONS)
  @FeatureRequirement(featureClass = Graph.Features.GraphFeatures.class, feature = FEATURE_THREADED_TRANSACTIONS, supported = false)
  public void shouldThrowOnThreadedTransactionNotSupported() {
    try {
      graph.tx().createThreadedTx();
      fail("An exception should be thrown since the threaded transaction feature is not supported");
    } catch (Exception e) {
      validateException(Transaction.Exceptions.threadedTransactionsNotSupported(), e);
    }
  }
}

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

@Test
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES)
@FeatureRequirement(featureClass = Graph.Features.GraphFeatures.class, feature = Graph.Features.GraphFeatures.FEATURE_TRANSACTIONS)
@FeatureRequirement(featureClass = Graph.Features.GraphFeatures.class, feature = Graph.Features.GraphFeatures.FEATURE_THREADED_TRANSACTIONS)
public void shouldOpenTxWhenThreadedTransactionIsCreated() throws Exception {
  // threaded transactions should be immediately open on creation
  final Graph threadedG = g.tx().createThreadedTx();
  assertThat(threadedG.tx().isOpen(), is(true));
  threadedG.tx().rollback();
  assertThat(threadedG.tx().isOpen(), is(false));
}

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

@Test
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES)
@FeatureRequirement(featureClass = Graph.Features.GraphFeatures.class, feature = Graph.Features.GraphFeatures.FEATURE_TRANSACTIONS)
@FeatureRequirement(featureClass = Graph.Features.GraphFeatures.class, feature = Graph.Features.GraphFeatures.FEATURE_THREADED_TRANSACTIONS)
public void shouldNotReuseThreadedTransaction() throws Exception {
  final int numberOfThreads = 10;
  final CountDownLatch latch = new CountDownLatch(numberOfThreads);
  final Graph threadedG = g.tx().createThreadedTx();
  for (int ix = 0; ix < numberOfThreads; ix++) {
    new Thread(() -> {
      threadedG.addVertex();
      latch.countDown();
    }).start();
  }
  latch.await(10000, TimeUnit.MILLISECONDS);
  // threaded transaction is not yet committed so g should not reflect any change
  assertVertexEdgeCounts(graph, 0, 0);
  threadedG.tx().commit();
  // there should be one vertex for each thread
  assertVertexEdgeCounts(graph, numberOfThreads, 0);
  try {
    assertThat(threadedG.tx().isOpen(), is(false));
    threadedG.addVertex();
    fail("Shouldn't be able to re-use a threaded transaction");
  } catch (Exception ex) {
    assertThat(ex, instanceOf(IllegalStateException.class));
  } finally {
    if (threadedG.tx().isOpen()) threadedG.tx().rollback();
  }
}

代码示例来源:origin: org.apache.tinkerpop/gremlin-test

@Test
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES)
@FeatureRequirement(featureClass = Graph.Features.GraphFeatures.class, feature = Graph.Features.GraphFeatures.FEATURE_TRANSACTIONS)
@FeatureRequirement(featureClass = Graph.Features.GraphFeatures.class, feature = Graph.Features.GraphFeatures.FEATURE_THREADED_TRANSACTIONS)
public void shouldSupportMultipleThreadsOnTheSameTransaction() throws Exception {
  final int numberOfThreads = 10;
  final CountDownLatch latch = new CountDownLatch(numberOfThreads);
  final Graph threadedG = g.tx().createThreadedTx();
  for (int ix = 0; ix < numberOfThreads; ix++) {
    new Thread(() -> {
      threadedG.addVertex();
      latch.countDown();
    }).start();
  }
  latch.await(10000, TimeUnit.MILLISECONDS);
  // threaded transaction is not yet committed so g should not reflect any change
  assertVertexEdgeCounts(graph, 0, 0);
  threadedG.tx().commit();
  // there should be one vertex for each thread
  assertVertexEdgeCounts(graph, numberOfThreads, 0);
}

代码示例来源:origin: com.syncleus.ferma/ferma

@Override
public WrappedFramedGraph<? extends Graph> createThreadedTx() {
  return new DelegatingFramedGraph<>(this.getDelegate().createThreadedTx(), this.getGraph().getBuilder(), this.getGraph().getTypeResolver());
}

代码示例来源:origin: Syncleus/Ferma

@Override
public WrappedFramedGraph<? extends Graph> createThreadedTx() {
  return new DelegatingFramedGraph<>(this.getDelegate().createThreadedTx(), this.getGraph().getBuilder(), this.getGraph().getTypeResolver());
}

代码示例来源:origin: org.apache.tinkerpop/gremlin-test

@Test
  @FeatureRequirement(featureClass = Graph.Features.GraphFeatures.class, feature = FEATURE_TRANSACTIONS)
  @FeatureRequirement(featureClass = Graph.Features.GraphFeatures.class, feature = FEATURE_THREADED_TRANSACTIONS, supported = false)
  public void shouldThrowOnThreadedTransactionNotSupported() {
    try {
      graph.tx().createThreadedTx();
      fail("An exception should be thrown since the threaded transaction feature is not supported");
    } catch (Exception e) {
      validateException(Transaction.Exceptions.threadedTransactionsNotSupported(), e);
    }
  }
}

代码示例来源:origin: org.apache.tinkerpop/gremlin-test

@Test
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES)
@FeatureRequirement(featureClass = Graph.Features.GraphFeatures.class, feature = Graph.Features.GraphFeatures.FEATURE_TRANSACTIONS)
@FeatureRequirement(featureClass = Graph.Features.GraphFeatures.class, feature = Graph.Features.GraphFeatures.FEATURE_THREADED_TRANSACTIONS)
public void shouldOpenTxWhenThreadedTransactionIsCreated() throws Exception {
  // threaded transactions should be immediately open on creation
  final Graph threadedG = g.tx().createThreadedTx();
  assertThat(threadedG.tx().isOpen(), is(true));
  threadedG.tx().rollback();
  assertThat(threadedG.tx().isOpen(), is(false));
}

代码示例来源:origin: org.apache.tinkerpop/gremlin-test

@Test
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES)
@FeatureRequirement(featureClass = Graph.Features.GraphFeatures.class, feature = Graph.Features.GraphFeatures.FEATURE_TRANSACTIONS)
@FeatureRequirement(featureClass = Graph.Features.GraphFeatures.class, feature = Graph.Features.GraphFeatures.FEATURE_THREADED_TRANSACTIONS)
public void shouldNotReuseThreadedTransaction() throws Exception {
  final int numberOfThreads = 10;
  final CountDownLatch latch = new CountDownLatch(numberOfThreads);
  final Graph threadedG = g.tx().createThreadedTx();
  for (int ix = 0; ix < numberOfThreads; ix++) {
    new Thread(() -> {
      threadedG.addVertex();
      latch.countDown();
    }).start();
  }
  latch.await(10000, TimeUnit.MILLISECONDS);
  // threaded transaction is not yet committed so g should not reflect any change
  assertVertexEdgeCounts(graph, 0, 0);
  threadedG.tx().commit();
  // there should be one vertex for each thread
  assertVertexEdgeCounts(graph, numberOfThreads, 0);
  try {
    assertThat(threadedG.tx().isOpen(), is(false));
    threadedG.addVertex();
    fail("Shouldn't be able to re-use a threaded transaction");
  } catch (Exception ex) {
    assertThat(ex, instanceOf(IllegalStateException.class));
  } finally {
    if (threadedG.tx().isOpen()) threadedG.tx().rollback();
  }
}

相关文章