|
| 1 | +.. _rust-transactions: |
| 2 | + |
| 3 | +============ |
| 4 | +Transactions |
| 5 | +============ |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: reference |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: code example, ACID compliance, multi-document |
| 13 | + |
| 14 | +.. contents:: On this page |
| 15 | + :local: |
| 16 | + :backlinks: none |
| 17 | + :depth: 2 |
| 18 | + :class: singlecol |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn how to use the {+driver-short+} to perform |
| 24 | +**transactions**. Transactions allow you to perform a series of operations |
| 25 | +that change data only if the entire transaction is committed. |
| 26 | +If any operation in the transaction does not succeed, the driver stops the |
| 27 | +transaction and discards all data changes before they ever become |
| 28 | +visible. This feature is called **atomicity**. |
| 29 | + |
| 30 | +In MongoDB, transactions run within logical sessions. A |
| 31 | +session is a grouping of related read or write operations that you |
| 32 | +want to run sequentially. Sessions enable causal consistency for a group |
| 33 | +of operations and allow you to run operations in an **ACID-compliant** |
| 34 | +transaction, which is a transaction that meets an expectation of |
| 35 | +atomicity, consistency, isolation, and durability. MongoDB guarantees |
| 36 | +that the data involved in your transaction operations remains |
| 37 | +consistent, even if the operations encounter unexpected errors. |
| 38 | + |
| 39 | +When using the {+driver-short+}, you can create a new session from a |
| 40 | +``Client`` instance as a ``ClientSession`` type. You can improve your |
| 41 | +app's performance by reusing your client for multiple sessions and |
| 42 | +transactions instead of instantiating a new client each time. |
| 43 | + |
| 44 | +.. warning:: |
| 45 | + |
| 46 | + Use a ``ClientSession`` only in operations running on the |
| 47 | + ``Client`` that created it. Using a ``ClientSession`` with a |
| 48 | + different ``Client`` results in operation errors. |
| 49 | + |
| 50 | +Methods |
| 51 | +------- |
| 52 | + |
| 53 | +Create a ``ClientSession`` by using the ``start_session()`` method on your |
| 54 | +``Client`` instance. You can then modify the session state using the |
| 55 | +methods provided by the ``ClientSession`` type. The following table |
| 56 | +describes these methods: |
| 57 | + |
| 58 | +.. list-table:: |
| 59 | + :widths: 25 75 |
| 60 | + :stub-columns: 1 |
| 61 | + :header-rows: 1 |
| 62 | + |
| 63 | + * - Method |
| 64 | + - Description |
| 65 | + |
| 66 | + * - ``start_transaction()`` |
| 67 | + - | Starts a new transaction, configured according to an optional |
| 68 | + ``TransactionOptions`` parameter, on this session. The session |
| 69 | + must be passed into each operation within the transaction, or |
| 70 | + the operation will run outside of the transaction. |
| 71 | + | |
| 72 | + | Errors returned from operations run within the transaction |
| 73 | + might include a ``TRANSIENT_TRANSACTION_ERROR`` label, which |
| 74 | + indicates that the entire transaction can be ended, then retried |
| 75 | + with the expectation that it will succeed. |
| 76 | + | |
| 77 | + | **Parameter**: ``TransactionOptions`` |
| 78 | + |
| 79 | + * - ``commit_transaction()`` |
| 80 | + - | Commits the active transaction for this session. This method returns an |
| 81 | + error if there is no active transaction for the session or the |
| 82 | + transaction was previously ended. |
| 83 | + | |
| 84 | + | This method might return an error that includes an |
| 85 | + ``UNKNOWN_TRANSACTION_COMMIT_RESULT`` label, which |
| 86 | + indicates that it is unknown if the committed transaction |
| 87 | + satisfies the set write concern. If you encounter this error, |
| 88 | + it is safe to retry the commit until the write concern is |
| 89 | + satisfied or the method returns an error without the label. |
| 90 | + |
| 91 | + * - ``abort_transaction()`` |
| 92 | + - | Ends the active transaction for this session. This method returns an |
| 93 | + error if there is no active transaction for the session or if the |
| 94 | + transaction was committed or ended. |
| 95 | + |
| 96 | + * - ``with_transaction()`` |
| 97 | + - | Starts a transaction on this session and runs the given |
| 98 | + callback, then commits or ends the transaction. When you use |
| 99 | + this method to perform a transaction, the driver automatically |
| 100 | + handles any errors, so you can choose to omit error handling code. |
| 101 | + | |
| 102 | + | Because the callback returns a future and can be run multiple |
| 103 | + times, the Rust language closure borrowing rules for captured |
| 104 | + values can be restrictive. So, the ``with_transaction()`` |
| 105 | + method accepts a context parameter that is passed to the callback. |
| 106 | + | |
| 107 | + | **Parameters**: context ``C``, callback ``FnMut(&'a mut |
| 108 | + ClientSession, &'a mut C)``, ``TransactionOptions`` |
| 109 | + |
| 110 | +.. important:: Methods That Can Run in Transactions |
| 111 | + |
| 112 | + To run MongoDB tasks within transactions, you must use the |
| 113 | + ``_with_session()`` suffixed methods. These methods accept a |
| 114 | + ``ClientSession`` instance as a parameter. |
| 115 | + |
| 116 | + For example, to delete a document, you can generally use the |
| 117 | + ``delete_one()`` method. However, to delete a document within |
| 118 | + a transaction, you must use the ``delete_one_with_session()`` |
| 119 | + method and pass the session as a parameter. |
| 120 | + |
| 121 | +Example |
| 122 | +------- |
| 123 | + |
| 124 | +The following code defines the ``insert_media()`` callback function that |
| 125 | +inserts data into the ``books`` and ``films`` collections: |
| 126 | + |
| 127 | +.. literalinclude:: /includes/fundamentals/code-snippets/transaction.rs |
| 128 | + :language: rust |
| 129 | + :dedent: |
| 130 | + :start-after: begin-callback |
| 131 | + :end-before: end-callback |
| 132 | + |
| 133 | +The following code completes the following actions to perform the |
| 134 | +transaction: |
| 135 | + |
| 136 | +1. Creates a session from the client by using the ``start_session()`` method. |
| 137 | +#. Uses the ``with_transaction()`` method to start a transaction and run |
| 138 | + the ``insert_media()`` callback function within the transaction. |
| 139 | + |
| 140 | +.. io-code-block:: |
| 141 | + |
| 142 | + .. input:: /includes/fundamentals/code-snippets/transaction.rs |
| 143 | + :language: rust |
| 144 | + :dedent: |
| 145 | + :start-after: begin-session |
| 146 | + :end-before: end-session |
| 147 | + |
| 148 | + .. output:: |
| 149 | + :language: console |
| 150 | + :visible: false |
| 151 | + |
| 152 | + Successfully committed transaction! |
| 153 | + |
| 154 | +If you require more control over your transactions, see the `ClientSession API |
| 155 | +documentation <{+api+}/struct.ClientSession.html#transactions>`__ |
| 156 | +to find an example that shows how to manually create and commit a transaction. |
| 157 | + |
| 158 | +Additional Information |
| 159 | +---------------------- |
| 160 | + |
| 161 | +To learn more about the concepts mentioned in this guide, see the |
| 162 | +following pages in the Server manual: |
| 163 | + |
| 164 | +- :manual:`Transactions </core/transactions/>` |
| 165 | +- :manual:`Server Sessions </reference/server-sessions/>` |
| 166 | +- :manual:`Read Isolation, Consistency, and Recency </core/read-isolation-consistency-recency/#causal-consistency>` |
| 167 | + |
| 168 | +To learn more about ACID compliance, see the :website:`What are ACID |
| 169 | +Properties in Database Management Systems? </basics/acid-transactions>` |
| 170 | +article on the MongoDB website. |
| 171 | + |
| 172 | +To learn more about insert operations, see the |
| 173 | +:ref:`rust-insert-guide` guide. |
| 174 | + |
| 175 | +API Documentation |
| 176 | +~~~~~~~~~~~~~~~~~ |
| 177 | + |
| 178 | +To learn more about the methods and types mentioned in this |
| 179 | +guide, see the following API documentation: |
| 180 | + |
| 181 | +- `ClientSession <{+api+}/struct.ClientSession.html#>`__ |
| 182 | +- `start_session() <{+api+}/struct.Client.html#method.start_session>`__ |
| 183 | +- `start_transaction() <{+api+}/struct.ClientSession.html#method.start_transaction>`__ |
| 184 | +- `commit_transaction() <{+api+}/struct.ClientSession.html#method.commit_transaction>`__ |
| 185 | +- `abort_transaction() <{+api+}/struct.ClientSession.html#method.abort_transaction>`__ |
| 186 | +- `with_transaction() <{+api+}/struct.ClientSession.html#method.with_transaction>`__ |
| 187 | +- `TransactionOptions <{+api+}/options/struct.TransactionOptions.html>`__ |
0 commit comments