Docs Menu
Docs Home
/ / /
Java Sync
/ /

Connect to MongoDB Using a JNDI Datasource

On this page

  • Overview

In this guide, you can learn how to connect the MongoDB Java driver to your MongoDB instance using a Java Naming and Directory Interface (JNDI) Datasource.

MongoClientFactory includes a JNDI ObjectFactory implementation that returns MongoClient instances based on a connection URI. Consult the following guides to learn how to configure your application to connect using a JNDI Datasource.

  1. In a Wildfly installation, create a new module for MongoDB at modules/system/layers/base/org/mongodb/main. Copy the following jar files into the module:

    • mongodb-driver-sync-4.7.2.jar

    • mongodb-driver-core-4.7.2.jar

    • bson-4.7.2.jar

    • bson-record-codec-4.7.2.jar

    Add the following module.xml file into the module:

    <module xmlns="urn:jboss:module:1.3" name="org.mongodb">
    <resources>
    <resource-root path="mongodb-driver-sync-4.1.0.jar"/>
    </resources>
    <dependencies>
    <module name="javax.api"/>
    <module name="javax.transaction.api"/>
    <module name="javax.servlet.api" optional="true"/>
    </dependencies>
    </module>
  2. Add a binding to the naming subsystem configuration that references the preceding module, the MongoClientFactory class, and the connection string for the MongoDB cluster.

    <subsystem xmlns="urn:jboss:domain:naming:2.0">
    <bindings>
    <object-factory name="java:global/MyMongoClient" module="org.mongodb" class="com.mongodb.client.MongoClientFactory">
    <environment>
    <property name="connectionString" value="<connection string uri>"/>
    </environment>
    </object-factory>
    </bindings>
    <remote-naming/>
    </subsystem>

    Note

    Replace the placeholder connection value in the property tag with a value that points to your MongoDB installation.

This makes a MongoClient instance accessible via the JNDI name MyMongoClient in the java:global context.

  1. Copy the following jar files into the lib directory of your Tomcat installation:

    • mongodb-driver-sync-4.7.2.jar

    • mongodb-driver-core-4.7.2.jar

    • bson-4.7.2.jar

    • bson-record-codec-4.7.2.jar

  2. In the context.xml file of your application, add a resource that references the MongoClientFactory class and the connection string for the MongoDB cluster:

    <Resource name="mongodb/MyMongoClient"
    auth="Container"
    type="com.mongodb.client.MongoClient"
    closeMethod="close"
    factory="com.mongodb.client.MongoClientFactory"
    singleton="true"
    connectionString="<connection string uri>"/>

    Note

    Replace the placeholder URI in the connectionString attribute with a value that points to your MongoDB installation.

  3. In web.xml of your application, add a reference to the MongoClientFactory resource defined in the previous step:

    <resource-ref>
    <res-ref-name>
    mongodb/MyMongoClient
    </res-ref-name>
    <res-type>
    com.mongodb.MongoClient
    </res-type>
    <res-auth>
    Container
    </res-auth>
    </resource-ref>

This makes a MongoClient instance accessible via the JNDI name mongodb/MyMongoClient in the java:comp/env context.

Tip

See also:

For JNDI implementations other than those listed here, you can create your own Factory wrapper based on the driver's built-in MongoClientFactory implementation.

Back

Enable TLS/SSL on a Connection

On this page