Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use a single MongoClient across a JavaEE web service

After reading the mongo documentation that says each instance of a MongoClient handles its own pooling, how would I go about only having one instance across my whole application?

This seems like it could be a scenario for using a singleton bean, but this seems like it would defeat the purpose of connection pooling. If only one user would be able to access the bean that contains the MongoClient instance at a time, surely multiple connections in the pool would never be used at the same time.

Have I got my understanding of singletons wrong, or is that indeed the right way to go about it?

like image 990
Matt Williams Avatar asked Sep 28 '14 12:09

Matt Williams


People also ask

Should MongoClient be Singleton?

Typically yes, it is a good practice to make MongoClient a singleton. As mentioned in the MongoDB Java driver: The MongoClient instance represents a pool of connections to the database; you will only need one instance of class MongoClient even with multiple threads.

How does MongoDB connect to MongoClient in Java?

To connect: MongoClient client = MongoClients. create("<<MongoDB URI>>"); To connect to MongoDB on your local instance and default port, you can just omit the URI part of the above, or use a URI like 'mongodb://localhost:27017'.

What is MongoClient in Java?

MongoClient is the interface between our java program and MongoDB server. MongoClient is used to create connection, connect to database, retrieve collection names and create/read/update/delete database, collections, document etc.

Can I use Java with MongoDB?

Connecting via Java. Assuming you've resolved your dependencies and you've set up your project, you're ready to connect to MongoDB from your Java application. Since MongoDB is a document database, you might not be surprised to learn that you don't connect to it via traditional SQL/relational DB methods like JDBC.


2 Answers

but this seems like it would defeat the purpose of connection pooling.If only one user would be able to access the bean that contains the MongoClient instance at a time, surely multiple connections in the pool would never be used at the same time.

The javadoc says:

The Java MongoDB driver is thread safe. If you are using in a web serving environment, for example, you should create a single MongoClient instance, and you can use it in every request. The MongoClient object maintains an internal pool of connections to the database (default maximum pool size of 100). For every request to the DB (find, insert, etc) the Java thread will obtain a connection from the pool, execute the operation, and release the connection. This means the connection (socket) used may be different each time.

So, when you create a singleton with the client in it. It can be re-used as mentioned in the Javadoc. No synchronization is required, since it is thread safe.

how would I go about only having one instance across my whole application?

One of the implementations could be:

public enum ConnectionFactory {
    CONNECTION;
    private MongoClient client = null;

    private ConnectionFactory() {
        try {
            client = new MongoClient();
        } catch (Exception e) {
            // Log it.
        }
    }

    public MongoClient getClient() {
        if (client == null)
            throw new RuntimeException();
        return client;
    }
}

and use the client as, throughout the application. Connection pooling will be taken care by the MongoClient as documented.

MongoClient client = ConnectionFactory.CONNECTION.getClient();

or use the @singleton annotation:

@Singleton
public class SingletonA {

}

Refer: http://tomee.apache.org/singleton-example.html

like image 70
BatScream Avatar answered Oct 16 '22 04:10

BatScream


Since you are in a java ee environment, the best way to implement this would be to use CDI producers:

@Stateless
public class ConnetionFactory {

  @ApplicationScoped
  @Produces
  public MongoClient mongoClient() {
    return new MongoClient();
  }
}

Then in every bean you want to use it in:

@Stateless
public class MyServiceBean {

  @Inject
  private MongoClient mongoClient;
}
like image 31
maress Avatar answered Oct 16 '22 04:10

maress