AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

ドライバーAPIによるアプリケーション内のトランザクションの処理

The Callback API:

The Core API:

  • トランザクションを開始し、トランザクションをコミットするには、明示的な呼び出しが必要です。

  • Does not incorporate error handling logic for TransientTransactionError and UnknownTransactionCommitResult, and instead provides the flexibility to incorporate custom error handling for these errors.

Callback API には次のロジックが組み込まれています。

MongoDB6.2 以降、サーバーはTransactionTooLargeForCache エラーを受信してもトランザクションを再試行しません。

Core transaction API には、次のラベルの付いたエラーの再試行ロジックは組み込まれていません。

次の例には、一時的なエラーの場合はトランザクションを再試行し、不明なコミット エラーの場合はコミットを再試行するロジックが組み込まれています。

MongoDB かリレーショナル データベースかにかかわらず、データベース システムに関係なく、トランザクションのコミット中にエラーを処理し、トランザクションの再試行ロジックを組み込む必要があります。

The individual write operations inside the transaction are not retryable, regardless of the value of retryWrites. If an operation encounters an error associated with the label "TransientTransactionError", such as when the primary steps down, the transaction as a whole can be retried.

  • Callback API には"TransientTransactionError"の再試行ロジックが組み込まれています。

  • Core transaction API には"TransientTransactionError"の再試行ロジックは組み込まれていません。 "TransientTransactionError"を処理するには、アプリケーションはエラーの再試行ロジックを明示的に組み込む必要があります。 一時的なエラーの再試行ロジックを組み込んだ例については、「 Core API の例 」を参照してください。

コミット操作は、再試行可能な書込み操作です。 コミット操作でエラーが発生した場合、MongoDB ドライバーはretryWritesの値に関係なくコミットを再試行します。

コミット操作で"UnknownTransactionCommitResult"というラベルの付いたエラーが発生した場合は、コミットを再試行できます。

  • Callback API には"UnknownTransactionCommitResult"の再試行ロジックが組み込まれています。

  • Core transaction API には"UnknownTransactionCommitResult"の再試行ロジックは組み込まれていません。 "UnknownTransactionCommitResult"を処理するには、アプリケーションはエラーの再試行ロジックを明示的に組み込む必要があります。 不明なコミット エラーの再試行ロジックを組み込んだ例については、「 Core API の例 」を参照してください。

バージョン6.2の新機能。

MongoDB 6.2 以降、サーバーはTransactionTooLargeForCacheエラーを受信してもトランザクションを再試行しません。 このエラーは、キャッシュが小さすぎて再試行が失敗する可能性があることを意味します。

transactionTooLargeForCacheThresholdしきい値のデフォルト値は0.75です。 トランザクションがキャッシュの 75% 以上を使用している場合、サーバーはトランザクションを再試行する代わりにTransactionTooLargeForCacheを返します。

MongoDB の以前のバージョンでは、サーバーは ではなくTemporarilyUnavailable WriteConflictまたはTransactionTooLargeForCache を返します。

エラーしきい値を変更するには、 setParameterコマンドを使用します。

The following mongosh methods are available for transactions:

注意

The mongosh example omits retry logic and robust error handling for simplicity's sake. For a more practical example of incorporating transactions in applications, see Transaction Error Handling instead.

// Create collections:
db.getSiblingDB("mydb1").foo.insertOne(
{abc: 0},
{ writeConcern: { w: "majority", wtimeout: 2000 } }
)
db.getSiblingDB("mydb2").bar.insertOne(
{xyz: 0},
{ writeConcern: { w: "majority", wtimeout: 2000 } }
)
// Start a session.
session = db.getMongo().startSession( { readPreference: { mode: "primary" } } );
coll1 = session.getDatabase("mydb1").foo;
coll2 = session.getDatabase("mydb2").bar;
// Start a transaction
session.startTransaction( { readConcern: { level: "local" }, writeConcern: { w: "majority" } } );
// Operations inside the transaction
try {
coll1.insertOne( { abc: 1 } );
coll2.insertOne( { xyz: 999 } );
} catch (error) {
// Abort transaction on error
session.abortTransaction();
throw error;
}
// Commit the transaction using write concern set at transaction start
session.commitTransaction();
session.endSession();