Configuring MongoDB connectivity in Liberty

Applications that use MongoDB can run on Liberty. For access to a MongoDB instance, the applications use the MongoDB Java™ driver and data sources that you configure for the server.

MongoDB (from humongous) is a scalable, high-performance, open source NoSQL database.

Stabilized feature: The mongodb-2.0 feature is stabilized. The MongoDB Java driver versions 2.10.0 to 2.14.2 that the feature supports are no longer in service. Instead of using the mongodb-2.0 feature, create a CDI producer for Mongo. The CDI producer can use any Mongo version that meets your requirements.

Before you begin

Only MongoDB Java driver versions 2.10.0 to 2.14.2 are supported. For SSL, the minimum is 2.11.0 and for certificate authentication the minimum is 2.12.0.

About this task

To enable an application to use MongoDB, you configure a shared library for the MongoDB Java driver and a library reference to the shared library in the server.xml file of Liberty. An application can access MongoDB directly from the application or through the mongodb-2.0 feature and mongoDB instance configurations in the server.xml file.

Procedure

  1. Install the MongoDB Java driver in a location that your application and the Liberty runtime can access.

    For example, place the MongoDB driver .jar file in the Liberty_profile_root/usr/servers/server_name/lib directory.

  2. Configure a shared library for the MongoDB driver .jar file in the server.xml file of the Liberty server.
    <library id="MongoLib">
       <file name="${server.config.dir}/lib/mongo.jar" />
    </library>
  3. Enable your application to access MongoDB, either by direct access from the application or by using the mongodb-2.0 feature.
    • Enable direct access to MongoDB from the application.
      1. Configure a library reference for the shared library in an application element in the server.xml file.
        <application ...>
           <classloader commonLibraryRef="MongoLib"/>
        </application>
        The application can now access the MongoDB APIs directly. If you want the application to use the runtime injection engine, continue with the next steps.
    • Configure the mongodb-2.0 feature, mongo, and mongoDB elements in the server.xml file.
      1. Add the mongodb-2.0 feature to the server.xml file.
        <featureManager>
           <feature>mongodb-2.0</feature>
           <feature>jndi-1.0</feature>
        </featureManager>
        The JNDI feature is only required when you use jndi to look up resources. If you use resource injection, it is not required.
      2. Configure a mongo element that has a reference to the shared library created in a previous step.
        <mongo id="mongo" libraryRef="MongoLib" />
      3. Configure the mongoDB element.
        <mongoDB jndiName="mongo/testdb" mongoRef="mongo" databaseName="db-test" />

        Configuring a JNDI name enables an application or the Liberty runtime to look up the MongoDB instance.

      4. Enable your application to access MongoDB.

        The following example shows both JNDI lookup and resource injection:

        public class TestServlet extends HttpServlet {
              @Resource(name = "mongo/testdb")
              protected DB db;
              ...
           protected void doGet(HttpServletRequest request,
                 HttpServletResponse response) throws ServletException, IOException {
           // Alternatively use InitialContext lookup
              DB lookup = (DB) new InitialContext().lookup("java:comp/env/mongo/testdb");
        ...
      5. If you are using JNDI lookup, add a resource environment reference to the web.xml file of your application:
        <resource-env-ref>
           <resource-env-ref-name>mongo/testdb</resource-env-ref-name>
           <resource-env-ref-type>com.mongodb.DB</resource-env-ref-type>
        </resource-env-ref>

What to do next

Test use of the MongoDB from your application.

For more information about the MongoDB element, see MongoDB Integration 2.0.