Immutable solution version
MongoDB Atlas: Java sync driver connection settings
Complete solution
The Java driver wants a ConnectionString or MongoClientSettings and try-with-resources cleanup. This skill gives the Atlas-ready pattern.
Java sync driver + Atlas
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
}Rules
- Prefer
MongoClientSettings.builder()over a bare connection string when you need pool or timeout tuning;applyConnectionStringkeeps the Atlas SRV parsing. - One
MongoClientper application. It is thread-safe and holds the pool. try-with-resources(or an explicitclose()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.
Verify
The ping at startup confirms URI, credentials, and IP access list before the app serves traffic.
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 1 · 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>