Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs Menu
Docs Home
/ / /
PHP ライブラリ マニュアル
/ /

MongoDB\with_transaction()

バージョン 1.5 の新機能

MongoDB\with_transaction()

指定されたクライアント セッションを使用してトランザクション内でコールバックを実行

function with_transaction(
MongoDB\Driver\Session $session,
callable $callback,
array $transactionOptions = []
): void
$session : MongoDB\Driver\Session
トランザクションの実行に使用されるクライアント セッション。
$callback : callable
A callback that will be run inside the transaction. The callback must accept a MongoDB\Driver\Session object as its first argument.
$transactionOptions : 配列
Transaction options, which will be passed to MongoDB\Driver\Session::startTransaction. See the extension documentation for a list of supported options.

This function is responsible for starting a transaction, invoking a callback, and committing a transaction. It also applies logic to retry this process after certain errors within a preset time limit. The callback is expected to execute one or more operations within the transactionby passing the callback's MongoDB\Driver\Session argument as an option to those operations; however, that is not enforced.

注意

Applications are strongly encouraged to use an idempotent callback, since it may be invoked multiple times if retryable errors are encountered during either callback execution or committing.

コールバックの実行中にスローされた例外は、キャッチされて評価されます。 例外にTransientTransactionErrorエラー ラベルが付いている場合、トランザクションは中止されて再開され、コールバックが再度呼び出されます。 その他の例外の場合、トランザクションは中止され、例外が再スローされて、 with_transaction()の呼び出し元にエラーが伝達されます。

コールバックが正常に実行されると、トランザクションはコミットされます。 UnknownTransactionCommitResult エラー ラベルの付いた例外が発生した場合、コミットは再試行されます。 TransientTransactionErrorエラー ラベルの例外が発生した場合、トランザクションは再起動され、制御はコールバックの呼び出しに戻ります。 その他の例外は再スローされて、 with_transaction()の呼び出し元にエラーが伝達されます。

コールバック実行またはコミット中にエラーが発生した場合、 with_transaction()が最初に呼び出されてから 120 秒未満が経過した場合にのみプロセスは再試行されます。 この時間制限は構成できません。 この時間の経過後、通常再試行される例外はすべて再スローされます。

MongoDB\Exception\InvalidArgumentException は、パラメータまたはオプションの解析に関連するエラーの場合は です。

拡張レベルのその他のエラー(例: 接続エラー)の場合は、 MongoDB\Driver\Exception\RuntimeException 。

戻る

delete_logger()

項目一覧