Skip to content

Commit

Permalink
DOCSP-41831: nested txnoptions struct
Browse files Browse the repository at this point in the history
  • Loading branch information
rustagir committed Aug 2, 2024
1 parent 64507f3 commit 101992b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
44 changes: 39 additions & 5 deletions source/fundamentals/transactions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,13 @@ instantiating a new client each time.
Implementations of ``Session`` are not safe for concurrent use by multiple `goroutines
<https://www.golang-book.com/books/intro/10>`__.

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
Expand Down Expand Up @@ -127,6 +128,39 @@ A ``Session`` also has methods to retrieve session
properties and modify mutable session properties. View the :ref:`API
documentation <api-docs-transaction>` 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
-------

Expand Down
9 changes: 9 additions & 0 deletions source/includes/fundamentals/code-snippets/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions source/whats-new.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 101992b

Please sign in to comment.