Lesson 2: Connecting to an Atlas Cluster in Java Applications / Learn

Video Transcript (English)

Hi and welcome! In this video, we’ll download and install the official Java MongoDB driver by using Maven. We’ll also connect a MongoClient instance in a Java application to an Atlas cluster by using a connection string. Finally, we’ll cover how to instantiate a single MongoClient that can be reused across all database requests.

Let’s get started by using Maven to install the Java driver.

Locate and open your Maven project’s pom.xml file. Then add the MongoDB driver to your dependencies. The artifactId and driver version must be specified correctly so that Maven can download the libraries from the repository. We’ll install the synchronous driver.

Note: we’re installing 4.7.1 here, but you’ll want to install the latest version of the Java driver.

Now that we have the driver installed, let’s go through the steps to connect the application to our Atlas cluster.

Before we can connect, we need to instruct our application to connect to the Atlas cluster by using the Java synchronous driver from the Maven repository. We must also have a valid connection string (URI) to our Atlas cluster.

To retrieve the connection string, navigate to the Projects dashboard in the Atlas UI. You should see this screen, which shows the project and cluster name.

Click the Connect button. This will open a new window, which lists four connection options: Connect with the MongoDB Shell, connect your application, connect using MongoDB Compass, and connect using VS Code.

Select the “Connect your application” option to obtain your connection string, also known as the Connection URI. We will also have an option to include the driver import statements if needed.

The Atlas connection string format includes the protocol, username, password, host, and options for retryable writes and write concerns.

Notice that this connection string specifies a DNS service, or SRV, record . The SRV record includes information about the ports and target hosts in the replica set.

Now that we’ve retrieved our connection string, let's connect a MongoClient instance in our Java application to our Atlas cluster.

First, we initialize a ConnectionString instance with the connection string URI.

Then, we initialize a MongoClientSettings instance by using the ConnectionString instance.

MongoClientSettings allows us to control the behavior of the MongoClient instance.

Next, serverApi sets the server API to use when sending commands to the server. It also sets the stable API version. Here, we construct a `ServerApi` instance by using the `ServerApi.Builder` helper class. We also specify a stable API version by using a constant from the `ServerApiVersion` class.

Now we’ve written the code to create a MongoClient instance in our Java application that connects to an Atlas cluster by using a URI string. Let’s connect our application to the cluster by running the Maven command. You will see a list of databases. Next, we can start working with MongoDB.

It’s important to note that an application should use a single MongoClient instance for all database requests.

A MongoClient instance automatically manages the connections that your application needs to MongoDB. Instantiating MongoClients is resource intensive, so avoid creating more than one.

There are several approaches to managing the life cycle of a MongoClient.

For example, you can use a singleton or your favorite Dependency Injection (DI) framework. If you implement a singleton within a multi-threaded application, make sure that your implementation is thread safe. The process is the same for both traditional and Function-as-a-Service (or FaaS) environments.

Well done!

You now know how to connect to an Atlas cluster from your Java application.

Here are some takeaways from this video:

MongoDB Java drivers are added to a Maven-based Java application project by using the pom.xml.

We need a valid connection string and the MongoDB Java driver to connect to our Atlas cluster.

The connection string is available in the Atlas UI.

An application should use a single MongoClient instance to communicate with a MongoDB cluster for performance reasons.

To create a single MongoClient instance for a Java application, we can use a singleton design pattern.