@@ -53,8 +53,8 @@ Methods
53
53
-------
54
54
55
55
Create a ``ClientSession`` by using the ``startSession()`` method on your
56
- ``MongoClient``instance. You can then modify the session state by using the
57
- methods provided by the ``ClientSession``. The following table details the
56
+ ``MongoClient`` instance. You can then modify the session state by using the
57
+ methods provided by the ``ClientSession``. The following table describes the
58
58
methods you can use to manage your transaction:
59
59
60
60
.. list-table::
@@ -66,12 +66,12 @@ methods you can use to manage your transaction:
66
66
67
67
* - ``startTransaction()``
68
68
- | Starts a new transaction for this session with the
69
- default transaction options. Use the ``TransactionOptions``
70
- parameter to start a transaction with given options. You
69
+ default transaction options. Pass an instance of ``TransactionOptions``
70
+ as a parameter to start a transaction with given options. You
71
71
cannot start a transaction if there's already an active transaction
72
- on the session.
72
+ running in the session.
73
73
|
74
- | **Parameter**: ``transactionOptions``
74
+ | **Parameter**: ``TransactionOptions transactionOptions``
75
75
76
76
* - ``abortTransaction()``
77
77
- | Ends the active transaction for this session. Returns an error
@@ -84,35 +84,38 @@ methods you can use to manage your transaction:
84
84
transaction was ended.
85
85
86
86
* - ``withTransaction()``
87
- - | Starts a new transaction for this session and runs the given function.
87
+ - | Starts a new transaction for this session and runs the given function. This
88
+ method handles retries, committing, and aborting transactions. Pass an
89
+ instance of ``TransactionBody`` as a parameter that defines the
90
+ operations you want to execute within the transaction.
88
91
|
89
- | **Parameter**: ``transactionBody``
92
+ | **Parameter**: ``TransactionBody<T> transactionBody``
90
93
91
94
A ``ClientSession`` also has methods to retrieve session properties and modify mutable
92
- session properties. View the `API documentation <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/ClientSession.html>`__
93
- to learn more about these methods.
95
+ session properties. View the :ref:`API Documentation` to learn more about these methods.
94
96
95
97
Example
96
98
-------
97
99
98
100
The following example demonstrates how you can create a session, create a transaction,
99
101
and commit a multi-document insert operation through the following steps:
100
102
101
- 1. Create a session from the client using the ``startSession()`` method.
102
- #. Set transaction options to configure transactional behavior.
103
+ 1. Create a session from the client by using the ``startSession()`` method.
104
+ #. Set transaction options to configure transaction behavior.
103
105
#. Use the ``withTransaction()`` method to start a transaction.
104
106
#. Insert multiple documents. The ``withTransaction()`` method executes, commits, and aborts
105
107
the transaction. If any operation results in errors, ``withTransaction()`` handles canceling
106
108
the transaction.
107
109
108
110
.. literalinclude:: /includes/fundamentals/code-snippets/Transaction.java
109
111
:language: java
112
+ :dedent:
110
113
:start-after: start transaction
111
114
:end-before: end transaction
112
115
113
116
If you require more control over your transactions, you can use the ``startTransaction()``
114
117
method. This method allows you to explicitly commit or abort the transaction and manually
115
- manage the transaction lifecycle.
118
+ manage the transaction lifecycle. See :ref:`Methods`.
116
119
117
120
Additional Information
118
121
----------------------
0 commit comments