Overview
In this guide, you can learn how to connect the MongoDB Java driver to your MongoDB instance by 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.
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-5.4.0.jarmongodb-driver-core-5.4.0.jarbson-5.4.0.jarbson-record-codec-5.4.0.jar
Add the following
module.xmlfile into the module:module.xml<module xmlns="urn:jboss:module:1.9" name="org.mongodb"> <resources> <resource-root path="bson-5.4.0.jar"/> <resource-root path="bson-record-codec-5.4.0.jar"/> <resource-root path="mongodb-driver-core-5.4.0.jar"/> <resource-root path="mongodb-driver-sync-5.4.0.jar"/> </resources> <dependencies> <module name="javax.api"/> <module name="javax.transaction.api"/> <module name="javax.servlet.api" optional="true"/> </dependencies> </module> Add a binding to the naming subsystem configuration that references the preceding module, the
MongoClientFactoryclass, 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
propertytag with a value that points to your MongoDB installation.
This makes a MongoClient instance discoverable through the JNDI name
MyMongoClient in the java:global context.
Copy the following jar files into the
libdirectory of your Tomcat installation:mongodb-driver-sync-5.4.0.jarmongodb-driver-core-5.4.0.jarbson-5.4.0.jarbson-record-codec-5.4.0.jar
In the
context.xmlfile of your application, add a resource that references theMongoClientFactoryclass 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
connectionStringattribute with a value that points to your MongoDB installation.In
web.xmlof your application, add a reference to theMongoClientFactoryresource 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 discoverable through the JNDI name
mongodb/MyMongoClient in the java:comp/env context.
Tip
For JNDI implementations other than those listed here, you can create
your own Factory wrapper based on the driver's built-in
MongoClientFactory
implementation.