8000 DOCSP-35923: csot page by rustagir · Pull Request #657 · mongodb/docs-java · GitHub
[go: up one dir, main page]

Skip to content

DOCSP-35923: csot page #657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
MK tech review 2
  • Loading branch information
rustagir committed Mar 26, 2025
commit d9e0b19d626f7d2c22b5073a77cece3ab2d8acaa
10 changes: 5 additions & 5 deletions source/connection/specify-connection-options/csot.txt
8000
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ set the ``timeoutMS`` option in the following ways:
``MongoClientSettings.Builder`` class
- Setting the ``timeoutMS`` parameter in your connection string

The following code examples set a client-level timeout of ``5`` seconds.
The following code examples set a client-level timeout of ``200`` milliseconds.
Select the :guilabel:`MongoClientSettings` or :guilabel:`Connection
String` tab to see the corresponding code.

Expand Down Expand Up @@ -100,7 +100,7 @@ accepted values for ``timeoutMS``:

If you specify the ``timeoutMS`` option, the driver automatically applies the
specified timeout to each server operation. The following code example specifies
a timeout of ``5`` seconds at the client level, and then calls the
a timeout of ``200`` milliseconds at the client level, and then calls the
``MongoCollection.insertOne()`` method:

.. literalinclude:: /includes/connect/CsotExample.java
Expand Down Expand Up @@ -177,7 +177,7 @@ configuration can override a client-level timeout configuration:
:dedent:
:emphasize-lines: 10

.. _java-csot-txn:
.. _java-csot-transaction:

Transactions
~~~~~~~~~~~~
Expand Down Expand Up @@ -213,8 +213,8 @@ transaction:

.. literalinclude:: /includes/connect/CsotExample.java
:language: java
:start-after: start-txn-timeout
:end-before: end-txn-timeout
:start-after: start-transaction-timeout
:end-before: end-transaction-timeout
:dedent:

To learn more about transactions, see the :ref:`java-fundamentals-transactions` guide.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ documentation <api-docs-transaction>` to learn more about these methods.

You can set a limit on amount of time that operations can take
to complete in your transactions. To learn more, see the
:ref:`java-csot-txn` section of the Limit Server Execution Time guide.
:ref:`java-csot-transaction` section of the Limit Server Execution Time guide.

Example
-------
Expand Down
32 changes: 14 additions & 18 deletions source/includes/connect/CsotExample.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
package org.example;

import static com.mongodb.client.model.Filters.eq;
import static com.mongodb.client.model.Filters.gte;
import static java.util.concurrent.TimeUnit.SECONDS;
import static java.util.concurrent.TimeUnit.MILLISECONDS;

import com.mongodb.ClientSessionOptions;
import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.TransactionOptions;
import com.mongodb.client.*;
import com.mongodb.client.cursor.TimeoutMode;
import com.mongodb.client.model.InsertOneOptions;
import com.mongodb.client.model.bulk.ClientBulkWriteOptions;
import com.mongodb.client.result.InsertOneResult;
import org.bson.Document;


public class CsotExample {

public static void main(String[] args) {
MongoClient mongoClient = new csot().mongoClientSettings();
MongoClient mongoClient = new CsotExample().mongoClientSettings();
}

private MongoClient mongoClientSettings(){
// start-mongoclientsettings
MongoClientSettings settings = MongoClientSettings.builder()
.applyConnectionString(new ConnectionString("<connection string>"))
.timeout(5L, SECONDS)
.timeout(200L, MILLISECONDS)
.build();

MongoClient mongoClient = MongoClients.create(settings);
Expand All @@ -37,7 +33,7 @@ private MongoClient mongoClientSettings(){

private MongoClient connectionString(){
// start-string
String uri = "<connection string>/?timeoutMS=5000";
String uri = "<connection string>/?timeoutMS=200";
MongoClient mongoClient = MongoClients.create(uri);
// end-string

Expand All @@ -48,7 +44,7 @@ private void operationTimeout(){
// start-operation-timeout
MongoClientSettings settings = MongoClientSettings.builder()
.applyConnectionString(new ConnectionString("<connection string>"))
.timeout(5L, SECONDS)
.timeout(200L, MILLISECONDS)
.build();

try (MongoClient mongoClient = MongoClients.create(settings)) {
Expand All @@ -64,21 +60,21 @@ private void overrideTimeout(){
// start-override
MongoClientSettings settings = MongoClientSettings.builder()
.applyConnectionString(new ConnectionString("<connection string>"))
.timeout(5L, SECONDS)
.timeout(200L, MILLISECONDS)
.build();

try (MongoClient mongoClient = MongoClients.create(settings)) {
MongoDatabase database = mongoClient.getDatabase("db");
MongoCollection<Document> collection = database
.getCollection("people")
.withTimeout(10L, SECONDS);
.withTimeout(300L, MILLISECONDS);

// ... perform operations on MongoCollection
}
// end-override
}

8000 private void txnTimeout(){
private void transactionTimeout(){
MongoClientSettings settings = MongoClientSettings.builder()
.applyConnectionString(new ConnectionString("<connection string>"))
.build();
Expand All @@ -90,26 +86,26 @@ private void txnTimeout(){

// start-session-timeout
ClientSessionOptions opts = ClientSessionOptions.builder()
.defaultTimeout(5L, SECONDS)
.defaultTimeout(200L, MILLISECONDS)
.build();

ClientSession session = mongoClient.startSession(opts);
// ... perform operations on ClientSession
// end-session-timeout

// start-txn-timeout
TransactionOptions txnOptions = TransactionOptions.builder()
.timeout(5L, SECONDS)
// start-transaction-timeout
TransactionOptions transactionOptions = TransactionOptions.builder()
.timeout(200L, MILLISECONDS)
.build();
// end-txn-timeout
// end-transaction-timeout
}

}

private void cursorTimeout(){
MongoClientSettings settings = MongoClientSettings.builder()
.applyConnectionString(new ConnectionString("<connection string>"))
.timeout(5L, SECONDS)
.timeout(200L, MILLISECONDS)
.build();

try (MongoClient mongoClient = MongoClients.create(settings)) {
Expand Down
4 changes: 2 additions & 2 deletions source/includes/fundamentals/code-snippets/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static void main(String[] args) {
MongoCollection<Document> collection = database.getCollection("books");

// Sets transaction options
TransactionOptions txnOptions = TransactionOptions.builder()
TransactionOptions transactionOptions = TransactionOptions.builder()
.writeConcern(WriteConcern.MAJORITY)
.build();

Expand All @@ -32,7 +32,7 @@ public static void main(String[] args) {
new Document("title", "Song of Solomon").append("author", "Toni Morrison")
));
return null; // Return value as expected by the lambda
}, txnOptions);
}, transactionOptions);
}
} catch (Exception e) {
e.printStackTrace();
Expand Down
Loading
0