

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# Neptune openCypher でのトランザクション
<a name="access-graph-opencypher-transactions"></a>

Amazon Neptune の openCypher 実装は、[Neptune によって定義されたトランザクションセマンティクスを使用します](transactions-neptune.md)。ただし、Bolt ドライバーによって提供される分離レベルは、以下のセクションで説明するように、Bolt トランザクションセマンティクスに特定の影響を及ぼします。

## 読み取り専用の Bolt トランザクションクエリ
<a name="access-graph-opencypher-transactions-ro"></a>

読み取り専用クエリの処理には、次のようなさまざまなトランザクションモデルと分離レベルがあります。

### 暗黙的な読み取り専用トランザクションクエリ
<a name="access-graph-opencypher-transactions-ro-implicit"></a>

読み取り専用の暗黙的トランザクションの例を次に示します。

```
public void executeReadImplicitTransaction()
{
  // end point
  final String END_POINT = "(End Point URL)";

  // read query
  final String READ_QUERY = "MATCH (n) RETURN n limit 10";

  // create the driver
  final Driver driver = GraphDatabase.driver(END_POINT, AuthTokens.none(),
          Config.builder().withEncryption()
                          .withTrustStrategy(TrustStrategy.trustSystemCertificates())
                          .build());

  // create the session config
  SessionConfig sessionConfig = SessionConfig.builder()
                                             .withFetchSize(1000)
                                             .withDefaultAccessMode(AccessMode.READ)
                                             .build();

  // run the query as access mode read
  driver.session(sessionConfig).readTransaction(new TransactionWork<String>()
    {
      final StringBuilder resultCollector = new StringBuilder();

      @Override
      public String execute(final Transaction tx)
      {
        // execute the query
        Result queryResult = tx.run(READ_QUERY);

        // Read the result
        for (Record record : queryResult.list())
        {
          for (String key : record.keys())
          {
            resultCollector.append(key)
                           .append(":")
                           .append(record.get(key).asNode().toString());
          }
        }
        return resultCollector.toString();
      }

    }
  );

  // close the driver.
  driver.close();
}
```

リードレプリカは読み取り専用クエリしか受け付けないため、リードレプリカに対するすべてのクエリは、セッション構成に設定されているアクセスモードに関係なく、読み取り暗黙のトランザクションとして実行されます。Neptune は、`SNAPSHOT` 分離セマンティクスでは、読み取り暗黙のトランザクションを[読み取り専用クエリ](transactions-neptune.md#transactions-neptune-read-only)として評価します。

障害が発生すると、読み取り暗黙のトランザクションはデフォルトで再試行されます。

### 読み取り専用トランザクションクエリをオートコミットする
<a name="access-graph-opencypher-transactions-ro-autocommit"></a>

読み取り専用のオートコミットトランザクションの例を次に示します。

```
public void executeAutoCommitTransaction()
{
  // end point
  final String END_POINT = "(End Point URL)";

  // read query
  final String READ_QUERY = "MATCH (n) RETURN n limit 10";

  // Create the session config.
  final SessionConfig sessionConfig = SessionConfig
    .builder()
    .withFetchSize(1000)
    .withDefaultAccessMode(AccessMode.READ)
    .build();

  // create the driver
  final Driver driver = GraphDatabase.driver(END_POINT, AuthTokens.none(),
    Config.builder()
          .withEncryption()
          .withTrustStrategy(TrustStrategy.trustSystemCertificates())
          .build());

  // result collector
  final StringBuilder resultCollector = new StringBuilder();

  // create a session
  final Session session = driver.session(sessionConfig);

  // run the query
  final Result queryResult = session.run(READ_QUERY);
  for (final Record record : queryResult.list())
  {
    for (String key : record.keys())
    {
      resultCollector.append(key)
                     .append(":")
                     .append(record.get(key).asNode().toString());
    }
  }

  // close the session
  session.close();

  // close the driver
  driver.close();
}
```

セッション設定でアクセスモードが `READ` に設定されている場合、Neptune は、`SNAPSHOT` 分離セマンティクスではオートコミットトランザクションクエリを[読み取り専用クエリ](transactions-neptune.md#transactions-neptune-read-only)として評価します。リードレプリカは読み取り専用クエリしか受け付けないことに注意してください。

セッション設定を渡さない場合、オートコミットクエリはデフォルトでミューテーションクエリを分離して処理されるため、アクセスモードを明示的に `READ` に設定したセッション設定を渡すことが重要です。

失敗した場合、読み取り専用のオートコミットクエリは再試行されません。

### 暗黙的な読み取り専用トランザクションクエリ
<a name="access-graph-opencypher-transactions-ro-explicit"></a>

以下は明示的な読み取り専用トランザクションの例です。

```
public void executeReadExplicitTransaction()
{
  // end point
  final String END_POINT = "(End Point URL)";

  // read query
  final String READ_QUERY = "MATCH (n) RETURN n limit 10";

  // Create the session config.
  final SessionConfig sessionConfig = SessionConfig
    .builder()
    .withFetchSize(1000)
    .withDefaultAccessMode(AccessMode.READ)
    .build();

  // create the driver
  final Driver driver = GraphDatabase.driver(END_POINT, AuthTokens.none(),
    Config.builder()
          .withEncryption()
          .withTrustStrategy(TrustStrategy.trustSystemCertificates())
          .build());

  // result collector
  final StringBuilder resultCollector = new StringBuilder();

  // create a session
  final Session session = driver.session(sessionConfig);

  // begin transaction
  final Transaction tx = session.beginTransaction();

  // run the query on transaction
  final List<Record> list = tx.run(READ_QUERY).list();

  // read the result
  for (final Record record : list)
  {
    for (String key : record.keys())
    {
      resultCollector
        .append(key)
        .append(":")
        .append(record.get(key).asNode().toString());
    }
  }

  // commit the transaction and for rollback we can use beginTransaction.rollback();
  tx.commit();

  // close the driver
  driver.close();
}
```

セッション設定でアクセスモードが `READ` に設定されている場合、Neptune は、`SNAPSHOT` 分離セマンティクスでは明示的な読み取り専用トランザクションを[読み取り専用クエリ](transactions-neptune.md#transactions-neptune-read-only)として評価します。リードレプリカは読み取り専用クエリしか受け付けないことに注意してください。

セッション設定を渡さない場合、明示的読み取り専用トランザクションは、デフォルトではミューテーションクエリ分離で処理されるため、アクセスモードを明示的に `READ` に設定したセッション設定を渡すことが重要です。

失敗した場合、読み取り専用の明示的なクエリはデフォルトで再試行されます。

## ミューテーション Bolt トランザクションクエリ
<a name="access-graph-opencypher-transactions-wr"></a>

読み取り専用クエリと同様、ミューテーションクエリの処理にはさまざまな方法があり、次のようなさまざまなトランザクションモデルと分離レベルがあります。

### 暗黙的なミューテーショントランザクションクエリ
<a name="access-graph-opencypher-transactions-wr-implicit"></a>

暗黙的なミューテーショントランザクションの例を示します。

```
public void executeWriteImplicitTransaction()
{
  // end point
  final String END_POINT = "(End Point URL)";

  // create node with label as label and properties.
  final String WRITE_QUERY = "CREATE (n:label {name : 'foo'})";

  // Read the vertex created with label as label.
  final String READ_QUERY = "MATCH (n:label) RETURN n";

  // create the driver
  final Driver driver = GraphDatabase.driver(END_POINT, AuthTokens.none(),
    Config.builder()
          .withEncryption()
          .withTrustStrategy(TrustStrategy.trustSystemCertificates())
          .build());

  // create the session config
  SessionConfig sessionConfig = SessionConfig
    .builder()
    .withFetchSize(1000)
    .withDefaultAccessMode(AccessMode.WRITE)
    .build();

  final StringBuilder resultCollector = new StringBuilder();

  // run the query as access mode write
  driver.session(sessionConfig).writeTransaction(new TransactionWork<String>()
  {
    @Override
    public String execute(final Transaction tx)
    {
      // execute the write query and consume the result.
      tx.run(WRITE_QUERY).consume();

      // read the vertex written in the same transaction
      final List<Record> list = tx.run(READ_QUERY).list();

      // read the result
      for (final Record record : list)
      {
        for (String key : record.keys())
        {
          resultCollector
            .append(key)
            .append(":")
            .append(record.get(key).asNode().toString());
        }
      }
      return resultCollector.toString();
    }
  }); // at the end, the transaction is automatically committed.

  // close the driver.
  driver.close();
}
```

ミューテーションクエリの一部として行われる読み取りは、[Neptune ミューテーショントランザクション](transactions-neptune.md#transactions-neptune-mutation)では通常の保証に従って `READ COMMITTED` 分離で実行されます。

セッション設定を具体的に渡すかどうかにかかわらず、そのトランザクションは常に書き込みトランザクションとして扱われます。

コンフリクトについては、「[ロック待機タイムアウトを使用した競合の解決](transactions-neptune.md#transactions-neptune-conflicts)」を参照してください。

### オートコミットミューテーショントランザクションクエリ
<a name="access-graph-opencypher-transactions-wr-autocommit"></a>

ミューテーションオートコミットクエリは、ミューテーション暗黙的トランザクションと同じ動作を継承します。

セッション設定を渡さなかった場合、トランザクションはデフォルトでは書き込みトランザクションとして扱われます。

失敗した場合、ミューテーションオートコミットクエリは、自動的に再試行されません。

### 明示的なミューテーショントランザクションクエリ
<a name="access-graph-opencypher-transactions-wr-explicit"></a>

明示的なミューテーショントランザクションの例を示します。

```
public void executeWriteExplicitTransaction()
{
  // end point
  final String END_POINT = "(End Point URL)";

  // create node with label as label and properties.
  final String WRITE_QUERY = "CREATE (n:label {name : 'foo'})";

  // Read the vertex created with label as label.
  final String READ_QUERY = "MATCH (n:label) RETURN n";

  // create the driver
  final Driver driver = GraphDatabase.driver(END_POINT, AuthTokens.none(),
    Config.builder()
          .withEncryption()
          .withTrustStrategy(TrustStrategy.trustSystemCertificates())
          .build());

  // create the session config
  SessionConfig sessionConfig = SessionConfig
    .builder()
    .withFetchSize(1000)
    .withDefaultAccessMode(AccessMode.WRITE)
    .build();

  final StringBuilder resultCollector = new StringBuilder();

  final Session session = driver.session(sessionConfig);

  // run the query as access mode write
  final Transaction tx = driver.session(sessionConfig).beginTransaction();

  // execute the write query and consume the result.
  tx.run(WRITE_QUERY).consume();

  // read the result from the previous write query in a same transaction.
  final List<Record> list = tx.run(READ_QUERY).list();

  // read the result
  for (final Record record : list)
  {
    for (String key : record.keys())
    {
      resultCollector
        .append(key)
        .append(":")
        .append(record.get(key).asNode().toString());
    }
  }

  // commit the transaction and for rollback we can use tx.rollback();
  tx.commit();

  // close the session
  session.close();

  // close the driver.
  driver.close();
}
```

明示的なミューテーションクエリは、暗黙的なミューテーショントランザクションと同じ動作を継承します。

セッション設定を渡さなかった場合、トランザクションはデフォルトでは書き込みトランザクションとして扱われます。

コンフリクトについては、「[ロック待機タイムアウトを使用した競合の解決](transactions-neptune.md#transactions-neptune-conflicts)」を参照してください。