Applications that use MongoDB can run on the Liberty profile.
For access to a MongoDB instance, the applications use the MongoDB Java™ driver and data sources that
you configure for the server.
Before you begin
Note: The Liberty
profile provides configuration support for MongoDB. MongoDB (from
"humongous")
is a scalable, high-performance, open source NoSQL database.
You
must use Version 2.10.0 or later of the MongoDB Java driver.
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 the Liberty profile. 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
- 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.
- Configure a shared library for the MongoDB driver .jar file
in the server.xml file of the Liberty profile
server.
<library id="MongoLib">
<file name="${server.config.dir}/lib/mongo.jar" />
</library>
- 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.
- 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 utilize the runtime
injection engine, continue with the next steps.
- Configure the mongodb-2.0 feature, mongo,
and mongoDB elements in the server.xml file.
- 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. It is not required if you
use resource injection.
- Configure a mongo element that has a reference to the shared library
created in a previous step.
<mongo id="mongo" libraryRef="MongoLib" />
- 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.
- 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");
...
- 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.
Optionally,
review additional configuration properties in the mongo and mongoDB
sections of
Liberty profile: Configuration elements in the server.xml file.