|
| 1 | +=========================== |
| 2 | +MongoDB\\with_transaction() |
| 3 | +=========================== |
| 4 | + |
| 5 | +.. default-domain:: mongodb |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 1 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. versionadded:: 1.5 |
| 14 | + |
| 15 | +Definition |
| 16 | +---------- |
| 17 | + |
| 18 | +.. phpmethod:: MongoDB\\with_transaction() |
| 19 | + |
| 20 | + Execute a callback within a transaction using the given client session |
| 21 | + |
| 22 | + .. code-block:: php |
| 23 | + |
| 24 | + function with_transaction(MongoDB\Driver\Session $session, callable $callback, array $transactionOptions = []): void |
| 25 | + |
| 26 | + This method has the following parameters: |
| 27 | + |
| 28 | + .. include:: /includes/apiargs/function-with_transaction-param.rst |
| 29 | + |
| 30 | +Behavior |
| 31 | +-------- |
| 32 | + |
| 33 | +This function is responsible for starting a transaction, invoking a callback, |
| 34 | +and committing a transaction. It also applies logic to retry this process after |
| 35 | +certain errors within a preset time limit. The callback is expected to execute |
| 36 | +one or more operations within the transactionby passing the callback's |
| 37 | +:php:`MongoDB\\Driver\\Session <mongodb-driver-session>` argument as an option to |
| 38 | +those operations; however, that is not enforced. |
| 39 | + |
| 40 | +.. note:: |
| 41 | + |
| 42 | + Applications are strongly encouraged to use an |
| 43 | + `idempotent <https://en.wikipedia.org/wiki/Idempotence>`_ callback, since it |
| 44 | + may be invoked multiple times if retryable errors are encountered during |
| 45 | + either callback execution or committing. |
| 46 | + |
| 47 | +Any exception thrown during execution of the callback will be caught and |
| 48 | +evaluated. If an exception has a ``TransientTransactionError`` error label, the |
| 49 | +transaction will be aborted, restarted, and the callback will be invoked again. |
| 50 | +For any other exception, the transaction will be aborted and the exception |
| 51 | +re-thrown to propagate the error to the caller of ``with_transaction()``. |
| 52 | + |
| 53 | +Following successful execution of the callback, the transaction will be |
| 54 | +committed. If an exception with an UnknownTransactionCommitResult error label is |
| 55 | +encountered, the commit will be retried. If an exception with a |
| 56 | +``TransientTransactionError`` error label is encountered, the transaction will |
| 57 | +be restarted and control will return to invoking the callback. Any other |
| 58 | +exception will be re-thrown to propagate the error to the caller of |
| 59 | +``with_transaction()``. |
| 60 | + |
| 61 | +When an error occurs during callback execution or committing, the process is |
| 62 | +only retried if fewer than 120 seconds have elapsed since ``with_transaction()`` |
| 63 | +was first called. This time limit is not configurable. After this time, any |
| 64 | +exception that would normally result in a retry attempt will instead be |
| 65 | +re-thrown. |
| 66 | + |
| 67 | +Errors/Exceptions |
| 68 | +----------------- |
| 69 | + |
| 70 | +.. include:: /includes/extracts/error-invalidargumentexception.rst |
| 71 | +.. include:: /includes/extracts/error-driver-runtimeexception.rst |
| 72 | + |
| 73 | +See Also |
| 74 | +-------- |
| 75 | + |
| 76 | +- :php:`MongoDB\\Driver\\Session::startTransaction <mongodb-driver-session.starttransaction>` |
| 77 | +- :php:`MongoDB\\Driver\\Session::commitTransaction <mongodb-driver-session.committransaction>` |
| 78 | +- :manual:`Transactions: Drivers API </core/transactions-in-applications>` documentation in the MongoDB manual |
| 79 | +- `Convenient API for Transactions <https://github.com/mongodb/specifications/blob/master/source/transactions-convenient-api/transactions-convenient-api.rst>`_ specification |
0 commit comments