|
| 1 | +.. _golang-transactions: |
| 2 | + |
| 3 | +============ |
| 4 | +Transactions |
| 5 | +============ |
| 6 | + |
| 7 | +.. default-domain:: mongodb |
| 8 | + |
| 9 | +.. contents:: On this page |
| 10 | + :local: |
| 11 | + :backlinks: none |
| 12 | + :depth: 2 |
| 13 | + :class: singlecol |
| 14 | + |
| 15 | +Overview |
| 16 | +-------- |
| 17 | + |
| 18 | +In this guide, you can learn how to use **transactions** with the |
| 19 | +{+driver-long+}. :manual:`Transactions </core/transactions/>` allow you |
| 20 | +to run a series of operations that do not change any data until the |
| 21 | +transaction is committed. If any operation in the transaction fails, the |
| 22 | +driver aborts the transaction and discards all data changes before they |
| 23 | +ever become visible. |
| 24 | + |
| 25 | +In MongoDB, transactions run within logical **sessions**. A |
| 26 | +:manual:`session </reference/server-sessions/>` is a |
| 27 | +grouping of related read or write operations that you intend to run |
| 28 | +sequentially. Sessions enable :manual:`causal consistency </core/read-isolation-consistency-recency/#causal-consistency>` |
| 29 | +for a group of operations or allow you to execute operations in an :website:`ACID |
| 30 | +transaction </basics/acid-transactions>`. MongoDB guarantees that the data |
| 31 | +involved in your transaction operations remains , even if the operations |
| 32 | +encounter unexpected errors. |
| 33 | + |
| 34 | +With the {+driver-short+}, you can create a new session from a |
| 35 | +``Client`` instance as a ``Session`` type. |
| 36 | + |
| 37 | +.. warning:: |
| 38 | + |
| 39 | + You should use a ``Session`` only with the ``Client`` (or associated |
| 40 | + ``Database`` or ``Collection``) that created it. Using a |
| 41 | + ``Session`` with a different ``Client`` will result in operation |
| 42 | + errors. |
| 43 | + |
| 44 | +.. warning:: |
| 45 | + |
| 46 | + Implementations of ``Session`` are not safe for concurrent use by multiple `goroutines |
| 47 | + <https://www.golang-book.com/books/intro/10>`__. |
| 48 | + |
| 49 | +Methods |
| 50 | +------- |
| 51 | + |
| 52 | +After you start a session using the ``StartSession()`` method, you can modify |
| 53 | +the session state using the method set provided by the ``Session`` interface. The |
| 54 | +following table describes these methods: |
| 55 | + |
| 56 | +.. list-table:: |
| 57 | + :widths: 25 75 |
| 58 | + :header-rows: 1 |
| 59 | + |
| 60 | + * - Method |
| 61 | + - Description |
| 62 | + |
| 63 | + * - ``StartTransaction()`` |
| 64 | + - | Starts a new transaction, configured with the given options, on |
| 65 | + this session. Returns an error if there is already |
| 66 | + a transaction in progress for the session. For more |
| 67 | + information, see the :manual:`manual entry </reference/method/Session.startTransaction/>`. |
| 68 | + | |
| 69 | + | **Parameter**: ``TransactionOptions`` |
| 70 | + | **Return Type**: ``error`` |
| 71 | + |
| 72 | + * - ``AbortTransaction()`` |
| 73 | + - | Aborts the active transaction for this session. Returns an |
| 74 | + error if there is no active transaction for the session or the |
| 75 | + transaction has been committed or aborted. For more |
| 76 | + information, see the :manual:`manual entry </reference/method/Session.abortTransaction/>`. |
| 77 | + | |
| 78 | + | **Parameter**: ``Context`` |
| 79 | + | **Return Type**: ``error`` |
| 80 | + |
| 81 | + * - ``CommitTransaction()`` |
| 82 | + - | Commits the active transaction for this session. Returns an |
| 83 | + error if there is no active transaction for the session or the |
| 84 | + transaction has been aborted. For more |
| 85 | + information, see the :manual:`manual entry </reference/method/Session.commitTransaction/>`. |
| 86 | + | |
| 87 | + | **Parameter**: ``Context`` |
| 88 | + | **Return Type**: ``error`` |
| 89 | + |
| 90 | + * - ``WithTransaction()`` |
| 91 | + - | Starts a transaction on this session and runs the ``fn`` |
| 92 | + callback. |
| 93 | + | |
| 94 | + | **Parameters**: ``Context``, ``fn func(ctx SessionContext)``, ``TransactionOptions`` |
| 95 | + | **Return Type**: ``interface{}``, ``error`` |
| 96 | + |
| 97 | + * - ``EndSession()`` |
| 98 | + - | Aborts any existing transactions and closes the session. |
| 99 | + | |
| 100 | + | **Parameter**: ``Context`` |
| 101 | + | **Return Type**: none |
| 102 | + |
| 103 | +The ``Session`` interface also has methods to retrieve session |
| 104 | +properties and modify mutable session properties. Find more information |
| 105 | +in the :ref:`API documentation <api-docs-transaction>`. |
| 106 | + |
| 107 | +Example |
| 108 | +------- |
| 109 | + |
| 110 | +The following example shows how you can create a session, create a |
| 111 | +transaction, and commit a multi-document insert operation through the |
| 112 | +following steps: |
| 113 | + |
| 114 | +1. Create a session from the client using the ``StartSession()`` method. |
| 115 | +#. Use the ``WithTransaction()`` method to start a transaction. |
| 116 | +#. Insert multiple documents. The ``WithTransaction()`` method executes the |
| 117 | + insert and commits the transaction. If any operation results in |
| 118 | + errors, ``WithTransaction()`` handles aborting the transaction. |
| 119 | +#. Close the transaction and session using the ``EndSession()`` method. |
| 120 | + |
| 121 | +.. literalinclude:: /includes/fundamentals/code-snippets/transaction.go |
| 122 | + :language: go |
| 123 | + :dedent: |
| 124 | + :emphasize-lines: 4,8,10-11 |
| 125 | + :start-after: start-session |
| 126 | + :end-before: end-session |
| 127 | + |
| 128 | +If you need more control over your transactions, you can find an example |
| 129 | +showing how to manually create, commit, and abort transactions in the |
| 130 | +`full code example <https://raw.githubusercontent.com/mongodb/docs-golang/{+docs-branch+}/source/includes/fundamentals/code-snippets/transaction.go>`__. |
| 131 | + |
| 132 | +Additional Information |
| 133 | +---------------------- |
| 134 | + |
| 135 | +For more information about insert operations, see the |
| 136 | +:ref:`golang-insert-guide` fundamentals page. |
| 137 | + |
| 138 | +For more information about write concerns, see the |
| 139 | +:ref:`golang-write-read-pref` fundamentals page. |
| 140 | + |
| 141 | +For an additional example using sessions and transactions with the {+driver-short+}, see the |
| 142 | +:website:`developer blog post on Multi-Document ACID Transactions |
| 143 | +</developer/languages/go/golang-multi-document-acid-transactions/>`. |
| 144 | + |
| 145 | +.. _api-docs-transaction: |
| 146 | + |
| 147 | +API Documentation |
| 148 | +~~~~~~~~~~~~~~~~~ |
| 149 | + |
| 150 | +To learn more about any of the types or methods discussed in this |
| 151 | +guide, see the following API Documentation: |
| 152 | + |
| 153 | +- `Session <{+api+}/mongo#Session>`__ |
| 154 | +- `Client <{+api+}/mongo#Client>`__ |
| 155 | +- `StartSession() <{+api+}/mongo#Client.StartSession>`__ |
| 156 | +- `TransactionOptions <{+api+}/mongo/options#TransactionOptions>`__ |
| 157 | +- `SetWriteConcern() <{+api+}/mongo/options#TransactionOptions.SetWriteConcern>`__ |
| 158 | +- `InsertMany() <{+api+}/mongo#Collection.InsertMany>`__ |
0 commit comments