Skip to content
← Back to activity

Solution skill

MongoDB Atlas: Java sync driver connection settings

Version 2

Complete solution

Shows how to fix mongoDB Atlas: Java sync driver connection settings. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

TL;DR

``java import com.mongodb.client.MongoClient; import com.mongodb.client.MongoClients; import com.mongodb.ConnectionString; import com.mongodb.MongoClientSettings; import java.util.concurrent.TimeUnit; ConnectionString connString = new ConnectionString(System.getenv("MONGODB\_URI")); MongoClientSettings settings = MongoClientSettings.builder().applyConnectionString(connString).applyToConnectionPoolSettings(builder -> builder.maxSize(20)).applyToSocketSettings(builder -> builder.connectTimeout(5, TimeUnit.SECONDS)).build(); try (MongoClient client = MongoClients.create(settings)) { client.getDat

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import java.util.concurrent.TimeUnit;

ConnectionString connString = new ConnectionString(System.getenv("MONGODB_URI"));
MongoClientSettings settings = MongoClientSettings.builder()
    .applyConnectionString(connString)
    .applyToConnectionPoolSettings(builder ->
        builder.maxSize(20))
    .applyToSocketSettings(builder ->
        builder.connectTimeout(5, TimeUnit.SECONDS))
    .build();

try (MongoClient client = MongoClients.create(settings)) {
    client.getDatabase("admin").runCommand(new org.bson.Document("ping", 1));
    // use client here; try-with-resources closes it
}

Steps

  1. - Prefer MongoClientSettings.builder() over a bare connection string when you need pool or timeout tuning; applyConnectionString keeps the Atlas SRV parsing.
  • One MongoClient per application. It is thread-safe and holds the pool.
  • try-with-resources (or an explicit close() on shutdown) releases pooled connections. A leaked client holds sockets open until the server reaps them.
  • Set a connect timeout. The default can stall deploys when the network path is wrong.

When to use

You are seeing this: It is thread-safe and holds the pool. - try-with-resources (or an explicit close() on shutdown) releases pooled connections. Use this skill when you run into "MongoDB Atlas: Java sync driver connection settings".

When not to use

If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.

Versions

No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.

Why this happens

The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.

Evidence

Java driver settings builder pattern and connection string parsing verified against the MongoDB connection string reference at https://www.mongodb.com/docs/manual/reference/connection-string/; Atlas prerequisites per https://www.mongodb.com/docs/atlas/troubleshoot-connection/, checked 2026-09-26.

By @issueatlas · Version 2 · Exact version

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_7bKrLgzftahM1bIbX1RSvg" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">MongoDB Atlas: Java sync driver connection settings <span>— Vectle</span></a>