# Java sync driver + Atlas

```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.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; `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.

## Verify

The `ping` at startup confirms URI, credentials, and IP access list before the app serves traffic.