Skip to content

Commit 9c97066

Browse files
committed
Merge pull request #680
2 parents a8d8708 + 7e0337c commit 9c97066

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
arg_name: param
2+
name: $session
3+
type: :php:`MongoDB\\Driver\\Session <mongodb-driver-session>`
4+
description: |
5+
A client session used to execute the transaction.
6+
interface: phpmethod
7+
operation: ~
8+
optional: false
9+
---
10+
arg_name: param
11+
name: $callback
12+
type: callback
13+
description: |
14+
A callback that will be run inside the transaction. The callback must accept a
15+
:php:`MongoDB\\Driver\\Session <mongodb-driver-session>` object as first
16+
argument.
17+
interface: phpmethod
18+
operation: ~
19+
optional: false
20+
---
21+
arg_name: param
22+
name: $transactionOptions
23+
type: array
24+
description: |
25+
Transaction options, which will be passed to
26+
:php:`MongoDB\\Driver\\Session::startTransaction <mongodb-driver-session.starttransaction>`.
27+
See the extension documentation for a list of supported options.
28+
interface: phpmethod
29+
operation: ~
30+
optional: true
31+
...

docs/reference.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ Reference
1515
/reference/write-result-classes
1616
/reference/result-classes
1717
/reference/enumeration-classes
18+
/reference/functions
1819
/reference/exception-classes
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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

docs/reference/functions.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=========
2+
Functions
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+
.. toctree::
14+
:titlesonly:
15+
16+
/reference/function/with_transaction

0 commit comments

Comments
 (0)