Skip to content

Commit 162eec3

Browse files
committed
review feedback
1 parent a5b488a commit 162eec3

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

source/fundamentals/transactions.txt

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ Methods
5353
-------
5454

5555
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
5858
methods you can use to manage your transaction:
5959

6060
.. list-table::
@@ -66,12 +66,12 @@ methods you can use to manage your transaction:
6666

6767
* - ``startTransaction()``
6868
- | 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
7171
cannot start a transaction if there's already an active transaction
72-
on the session.
72+
running in the session.
7373
|
74-
| **Parameter**: ``transactionOptions``
74+
| **Parameter**: ``TransactionOptions transactionOptions``
7575

7676
* - ``abortTransaction()``
7777
- | Ends the active transaction for this session. Returns an error
@@ -84,35 +84,38 @@ methods you can use to manage your transaction:
8484
transaction was ended.
8585

8686
* - ``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.
8891
|
89-
| **Parameter**: ``transactionBody``
92+
| **Parameter**: ``TransactionBody<T> transactionBody``
9093

9194
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.
9496

9597
Example
9698
-------
9799

98100
The following example demonstrates how you can create a session, create a transaction,
99101
and commit a multi-document insert operation through the following steps:
100102

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.
103105
#. Use the ``withTransaction()`` method to start a transaction.
104106
#. Insert multiple documents. The ``withTransaction()`` method executes, commits, and aborts
105107
the transaction. If any operation results in errors, ``withTransaction()`` handles canceling
106108
the transaction.
107109

108110
.. literalinclude:: /includes/fundamentals/code-snippets/Transaction.java
109111
:language: java
112+
:dedent:
110113
:start-after: start transaction
111114
:end-before: end transaction
112115

113116
If you require more control over your transactions, you can use the ``startTransaction()``
114117
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`.
116119

117120
Additional Information
118121
----------------------

source/includes/fundamentals/code-snippets/Transaction.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
public class Transaction {
1313
public static void main(String[] args) {
1414
// Connect to MongoDB
15-
String connectionString = "mongodb://localhost:27017"; // Update with your MongoDB URI
15+
String connectionString = "<connection string>"; // Replace with your connection string
1616
try (MongoClient mongoClient = MongoClients.create(connectionString)) {
17-
MongoDatabase database = mongoClient.getDatabase("yourDatabaseName");
18-
MongoCollection<Document> collection = database.getCollection("yourCollectionName");
19-
// start transaction
17+
MongoDatabase database = mongoClient.getDatabase("transaction_db");
18+
MongoCollection<Document> collection = database.getCollection("books");
19+
// start transaction
2020
// Set transaction options
2121
TransactionOptions txnOptions = TransactionOptions.builder()
2222
.writeConcern(WriteConcern.MAJORITY)
@@ -37,6 +37,6 @@ public static void main(String[] args) {
3737
} catch (Exception e) {
3838
e.printStackTrace();
3939
}
40-
// end transaction
40+
// end transaction
4141
}
4242
}

0 commit comments

Comments
 (0)