Docs Menu
Docs Home
/ / /
Java Reactive Streams ドライバー
/

トランザクション

項目一覧

  • Overview
  • サンプル データ
  • トランザクション メソッド
  • トランザクションの例
  • 詳細情報
  • API ドキュメント

このガイドでは、Java Reactive Streams ドライバーを使用してトランザクションを実行する方法を学習できます。トランザクション を使用すると、すべてのデータ変更が成功するまで適用されない一連の操作を実行できます。トランザクション内のいずれかの操作が失敗した場合、ドライバーはトランザクションをキャンセルし、表示されることなくすべてのデータ変更を破棄します。

MongoDBでは、トランザクションは論理セッション内で実行されます。セッションは 、順番に実行されるよう関連付けられた読み取り操作または書き込み操作のグループです。セッションを使用すると、操作のグループに対して因果整合性を有効にし、 ACIDトランザクションを実行できます。 MongoDBは、トランザクション操作で予期せぬエラーが発生した場合でも、その操作に関わるデータの一貫性が保たれることを保証します。

Java Reactive Streams ドライバーを使用すると、 MongoClientインスタンスからClientSession型の新しいセッションを作成できます。毎回新しいクライアントをインスタンス化するのではなく、クライアントを複数のセッションやトランザクションで再利用することを推奨します。

警告

ClientSession は、それを作成した MongoClient(または関連付けられたMongoDatabase または MongoCollection)でのみ使用します。ClientSession と、異なる MongoClient を使用すると、操作エラーが発生します。

このガイドの例では、sample_restaurants.restaurants sample_mflix.moviesAtlasサンプルデータセット の コレクションと コレクションを使用します。無料のMongoDB Atlasクラスターを作成し、サンプルデータセットをロードする方法については、「 はじめに 」を参照してください。

重要

プロジェクトリ アクター ライブラリ

このガイドでは、プロジェクト ReactivePublisher ライブラリを使用して、Java Reactive Streams ドライバー メソッドによって返された インスタンスを消費します。 Project Reactive ライブラリとその使用方法の詳細については、「 使用 開始 」を 参照してください。 (Reactor ドキュメントの参照)。このガイドで Project React ライブラリ メソッドを使用する方法の詳細については、「 MongoDB へのデータの書込み 」ガイドを参照してください。

MongoClientインスタンスでstartSession()メソッドを使用してClientSessionを作成します。次に、 ClientSessionが提供するメソッドを使用して、セッション状態を変更できます。次の表は、トランザクションを管理するために使用できる方法の詳細を示しています。

方式
説明
startTransaction()
Starts a new transaction, configured with the given options, on this session. Throws an exception if there is already a transaction in progress for the session. To learn more about this method, see the startTransaction() page in the MongoDB Server manual.
abortTransaction()
Ends the active transaction for this session. Throws an exception if there is no active transaction for the session or if the transaction is already committed or ended. To learn more about this method, see the abortTransaction() page in the MongoDB Server manual.
commitTransaction()
Commits the active transaction for this session. Throws an exception if there is no active transaction for the session or if the transaction was ended. To learn more about this method, see the commitTransaction() page in the MongoDB Server manual.

次の例は、セッションを作成し、トランザクションを作成し、1 つのトランザクションで複数のコレクションにドキュメントを挿入する方法を示しています。このコードでは、次の手順が実行されます。

  1. startSession()メソッドを使用してクライアントからセッションを作成します

  2. startTransaction()メソッドを使用してトランザクションを開始します

  3. コレクションと コレクションにドキュメントを挿入しますrestaurantsmovies

  4. commitTransaction()メソッドを使用してトランザクションをコミットします

MongoClient mongoClient = MongoClients.create(settings);
MongoDatabase restaurantsDatabase = mongoClient.getDatabase("sample_restaurants");
MongoCollection<Document> restaurants = restaurantsDatabase.getCollection("restaurants");
MongoDatabase moviesDatabase = mongoClient.getDatabase("sample_mflix");
MongoCollection<Document> movies = moviesDatabase.getCollection("movies");
Mono.from(mongoClient.startSession())
.flatMap(session -> {
// Begins the transaction
session.startTransaction();
// Inserts documents in the given order
return Mono.from(restaurants.insertOne(session, new Document("name", "Reactive Streams Pizza").append("cuisine", "Pizza")))
.then(Mono.from(movies.insertOne(session, new Document("title", "Java: Into the Streams").append("type", "Movie"))))
// Commits the transaction
.flatMap(result -> Mono.from(session.commitTransaction())
.thenReturn(result))
.onErrorResume(error -> Mono.from(session.abortTransaction()).then(Mono.error(error)))
.doFinally(signalType -> session.close());
})
// Closes the client after the transaction completes
.doFinally(signalType -> mongoClient.close())
// Prints the results of the transaction
.subscribe(
result -> System.out.println("Transaction succeeded"),
error -> System.err.println("Transaction failed: " + error)
);

このガイドで言及されている概念の詳細については、サーバー マニュアルの次のページを参照してください。

このガイドで説明した型やメソッドの詳細については、次の API ドキュメントを参照してください。

戻る

一括書き込み操作