diff --git a/source/fundamentals/transactions.txt b/source/fundamentals/transactions.txt index fccb4db4..f2fa1305 100644 --- a/source/fundamentals/transactions.txt +++ b/source/fundamentals/transactions.txt @@ -54,12 +54,13 @@ instantiating a new client each time. Implementations of ``Session`` are not safe for concurrent use by multiple `goroutines `__. -Methods -------- +Session Methods +--------------- -After you start a session by using the ``StartSession()`` method, you can modify -the session state by using the method set provided by the returned ``Session``. The -following table describes these methods: +After you start a session by using the ``StartSession()`` method on your +client, you can modify the session state by using the method set +provided by the returned ``Session``. The following table describes +these methods: .. list-table:: :widths: 25 75 @@ -127,6 +128,39 @@ A ``Session`` also has methods to retrieve session properties and modify mutable session properties. View the :ref:`API documentation ` to learn more about these methods. +.. _golang-session-txn-options: + +Session and Transaction Options +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +You can set options at the session level and the transaction level to +customize how the driver performs a transaction. The following steps +describe how to set options for all transactions run within a given +``Session``: + +1. Create a ``TransactionOptions`` instance. You can specify options + such as a write concern, read concern, and read preference for all + transactions run in a given session. + +#. Create a ``SessionOptions`` instance by calling the + ``SetDefaultTransactionOptions()`` method, passing the + ``TransactionOptions`` instance as the parameter. + + You can also specify other session options such as enabling causal + consistency. + +#. Pass the ``SessionOptions`` instance to the ``client.StartSession()`` + method. + +The following code demonstrates how to specify session and transaction +options, then create a session with these options: + +.. literalinclude:: /includes/fundamentals/code-snippets/transaction.go + :language: go + :dedent: + :start-after: begin-session-txn-options + :end-before: end-session-txn-options + Example ------- diff --git a/source/includes/fundamentals/code-snippets/transaction.go b/source/includes/fundamentals/code-snippets/transaction.go index 9d45a07f..b3813a02 100644 --- a/source/includes/fundamentals/code-snippets/transaction.go +++ b/source/includes/fundamentals/code-snippets/transaction.go @@ -55,6 +55,15 @@ func main() { fmt.Printf("Inserted _id values: %v\n", result) + // begin-session-txn-options + txnOpts := options.Transaction().SetReadConcern(readconcern.Majority()) + sessOpts := options.Session().SetDefaultTransactionOptions(txnOpts) + session, err := client.StartSession(sessOpts) + if err != nil { + return err + } + // end-session-txn-options + // MANUAL TRANSACTION EXAMPLE // uncomment this section to run this code diff --git a/source/whats-new.txt b/source/whats-new.txt index 9988f31f..e52f2dd5 100644 --- a/source/whats-new.txt +++ b/source/whats-new.txt @@ -92,6 +92,12 @@ The 2.0 {+driver-short+} release includes the following improvements and fixes: contained the number of dropped indexes. See the :ref:`golang-remove-index` section of the Indexes guide to learn more. +- You can set options for transactions run in a given session by + creating a ``TransactionOptions`` instance and passing the instance to + the ``SetDefaultTransactionOptions()`` method of ``SessionOptions``. + To learn more, see the :ref:`golang-session-txn-options` section of + the Transactions guide. + .. _version-1.16: What's New in 1.16