@@ -38,29 +38,50 @@ consistent, even if the operations encounter unexpected errors.
38
38
39
39
When using the {+php-library+}, you can create a new session from a
40
40
``MongoDB\Client`` instance. Then, you can use the resulting
41
- ``MongoDB\Driver\Session`` instance to perform transactions. You can
42
- improve your app's performance by reusing your client for multiple
43
- sessions and transactions instead of instantiating a new client each
44
- time.
41
+ ``MongoDB\Driver\Session`` instance to perform transactions.
45
42
46
43
.. warning::
47
44
48
45
Use a ``Session`` only in operations running on the
49
46
``Client`` that created it. Using a ``Session`` with a
50
47
different ``Client`` results in operation errors.
51
48
52
- Methods
53
- -------
49
+ Transaction APIs
50
+ ----------------
54
51
55
- Create a ``Session`` by using the ``MongoDB\Client::startSession()``
56
- method on your ``Client`` instance. The {+php-library+} provides a
57
- Convenient Transaction API to manage the transaction lifecyle. Use the
58
- ``MongoDB\with_transaction()`` method to run custom callback within a
59
- transaction. The ``with_transaction()`` method starts the transaction,
60
- then either commits it or ends it if there are errors. The
61
- :ref:`php-txn-example` section of this guide demonstrates how to use
52
+ In this section, you can learn about the transaction APIs provided by
53
+ the {+php-library+}. Before you begin a transaction, you must create a
54
+ ``Session`` by using the ``MongoDB\Client::startSession()``
55
+ method on your ``Client`` instance. Then, you can use either of the
56
+ following APIs to perform a transaction:
57
+
58
+ - :ref:`php-convenient-txn`
59
+ - :ref:`php-core-txn`
60
+
61
+ .. _php-convenient-txn:
62
+
63
+ Convenient API
64
+ ~~~~~~~~~~~~~~
65
+
66
+ The {+php-library+} provides a **Convenient Transaction API** to manage
67
+ the transaction lifecyle. Implement this API by using the
68
+ ``MongoDB\with_transaction()`` function to run custom callback within a
69
+ transaction. The ``with_transaction()`` function performs the following
70
+ tasks:
71
+
72
+ - Starts the transaction
73
+ - Handles errors by either ending the transaction or retrying it, such
74
+ as when the operation returns a ``TransientTransactionError``
75
+ - Commits the transaction
76
+
77
+ The :ref:`php-txn-example` section of this guide demonstrates how to use
62
78
this API to perform a transaction.
63
79
80
+ .. _php-core-txn:
81
+
82
+ Core API
83
+ ~~~~~~~~
84
+
64
85
Alternatively, you can have more control over your transaction lifecyle
65
86
by using the methods provided by the ``Session`` class. The
66
87
following table describes these methods:
@@ -82,8 +103,9 @@ following table describes these methods:
82
103
83
104
* - ``commitTransaction()``
84
105
- | Commits the active transaction for this session. This method returns an
85
- error if there is no active transaction for the session or the
86
- transaction was previously ended.
106
+ error if there is no active transaction for the session, the
107
+ transaction was previously ended, or if there is a write
108
+ conflict.
87
109
88
110
* - ``abortTransaction()``
89
111
- | Ends the active transaction for this session. This method returns an
@@ -103,8 +125,8 @@ banking transaction. The code performs the following actions:
103
125
collections.
104
126
- Specifies the account number and amount to be transferred between
105
127
accounts.
106
- - Defines the callback function, passing the ``Session`` instance as a
107
- parameter.
128
+ - Defines the callback function, which receives the ``Session`` instance
129
+ as a parameter.
108
130
- Updates the customer's balances to reflect the money transfer.
109
131
- Records a receipt of the transaction with a timestamp.
110
132
- Prints a message if the transaction committed successfully.
@@ -120,7 +142,7 @@ Then, run the following code to perform the transaction. This code
120
142
completes the following actions:
121
143
122
144
1. Creates a session from the client by using the ``startSession()`` method.
123
- #. Calls the ``with_transaction()`` method to manage the transaction,
145
+ #. Calls the ``with_transaction()`` function to manage the transaction,
124
146
passing the session and the callback as parameters.
125
147
126
148
.. io-code-block::
@@ -173,11 +195,7 @@ guide, see the following API documentation:
173
195
To learn more about the ``Session`` class and methods,
174
196
see the following {+extension-short+} API documentation:
175
197
176
- - `MongoDB\\Driver\\Session
177
- <{+php-manual+}/class.mongodb-driver-session.php>`__
178
- - `MongoDB\\Driver\\Session::abortTransaction()
179
- <{+php-manual+}/mongodb-driver-session.aborttransaction.php>`__
180
- - `MongoDB\\Driver\\Session::commitTransaction()
181
- <{+php-manual+}/mongodb-driver-session.committransaction.php>`__
182
- - `MongoDB\\Driver\\Session::startTransaction()
183
- <{+php-manual+}/mongodb-driver-session.starttransaction.php>`__
198
+ - :php:`MongoDB\Driver\Session <mongodb-driver-session>`
199
+ - :php:`MongoDB\Driver\Session::abortTransaction() <mongodb-driver-session.aborttransaction>`
200
+ - :php:`MongoDB\Driver\Session::commitTransaction() <mongodb-driver-session.committransaction>`
201
+ - :php:`MongoDB\Driver\Session::startTransaction() <mongodb-driver-session.starttransaction>`
0 commit comments