8000 txn code examples · rustagir/docs-java@739a254 · GitHub
[go: up one dir, main page]

Skip to content

Commit 739a254

Browse files
committed
txn code examples
1 parent f941168 commit 739a254

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

source/connection/specify-connection-options/csot.txt

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ You can specify a ``timeoutMS`` option at a lower level to override the
164164
client-level configuration. This allows you to customize timeouts based
165165
on the needs of individual operations.
166166

167-
The following example demonstrates how an collection-level timeout
167+
The following example demonstrates how a collection-level timeout
168168
configuration can override a client-level timeout configuration:
169169

170170
.. literalinclude:: /includes/connect/CSOT.java
@@ -191,13 +191,28 @@ the following methods:
191191
- `withTransaction() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/ClientSession.html#withTransaction(com.mongodb.client.TransactionBody)>`__
192192
- `close() <{+api+}/apidocs/mongodb-driver-core/com/mongodb/session/ClientSession.html#close()>`__
193193

194+
The following code demonstrates how to set the ``defaultTimeout`` when
195+
instantiating a ``ClientSession``:
196+
197+
.. literalinclude:: /includes/connect/CSOT.java
198+
:language: java
199+
:start-after: start-session-timeout
200+
:end-before: end-session-timeout
201+
:dedent:
202+
194203
If you do not specify the ``defaultTimeout``, the driver uses the timeout
195204
value set on the parent ``MongoClient``.
196205

197206
You can also set a transaction-level timeout by using the ``timeout()``
198207
method when building a ``TransactionOptions`` instance. Setting this
199208
option applies a timeout to all operations performed in the scope of the
200-
transaction.
209+
transaction:
210+
211+
.. literalinclude:: /includes/connect/CSOT.java
212+
:language: java
213+
:start-after: start-txn-timeout
214+
:end-before: end-txn-timeout
215+
:dedent:
201216

202217
To learn more about transactions, see the :ref:`java-fundamentals-transactions` guide.
203218

source/includes/connect/CSOT.java

Lines changed: 31 additions & 0 deletions
< 8000 td data-grid-cell-id="diff-0fe3afb39e92c4a6dbc7aa57289b72426de60607ab7b8846e673bd6f11926c95-14-17-2" data-line-anchor="diff-0fe3afb39e92c4a6dbc7aa57289b72426de60607ab7b8846e673bd6f11926c95R17" data-selected="false" role="gridcell" style="background-color:var(--bgColor-default);padding-right:24px" tabindex="-1" valign="top" class="focusable-grid-cell diff-text-cell right-side-diff-cell left-side">

Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
import static com.mongodb.client.model.Filters.gte;
55
import static java.util.concurrent.TimeUnit.SECONDS;
66

7+
import com.mongodb.ClientSessionOptions;
78
import com.mongodb.ConnectionString;
89
import com.mongodb.MongoClientSettings;
10+
import com.mongodb.TransactionOptions;
911
import com.mongodb.client.*;
1012
import com.mongodb.client.cursor.TimeoutMode;
1113
import com.mongodb.client.model.InsertOneOptions;
14+
import com.mongodb.client.model.bulk.ClientBulkWriteOptions;
1215
import com.mongodb.client.result.InsertOneResult;
1316
import org.bson.Document;
1417
@@ -75,6 +78,34 @@ private void overrideTimeout(){
7578
// end-override
7679
}
7780

81+
private void txnTimeout(){
82+
MongoClientSettings settings = MongoClientSettings.builder()
83+
.applyConnectionString(new ConnectionString("<connection string>"))
84+
.build();
85+
86+
try (MongoClient mongoClient = MongoClients.create(settings)) {
87+
MongoCollection<Document> collection = mongoClient
88+
.getDatabase("db")
89+
.getCollection("people");
90+
91+
// start-session-timeout
92+
ClientSessionOptions opts = ClientSessionOptions.builder()
93+
.defaultTimeout(5L, SECONDS)
94+
.build();
95+
96+
ClientSession session = mongoClient.startSession(opts);
97+
// ... perform operations on ClientSession
98+
// end-session-timeout
99+
100+
// start-txn-timeout
101+
TransactionOptions txnOptions = TransactionOptions.builder()
102+
.timeout(5L, SECONDS)
103+
.build();
104+
// end-txn-timeout
105+
}
106+
107+
}
108+
78109
private void cursorTimeout(){
79110
MongoClientSettings settings = MongoClientSettings.builder()
80111
.applyConnectionString(new ConnectionString("<connection string>"))

0 commit comments

Comments
 (0)
0