3

For example a database such as MongoDB. I doubt that it is unnecessary to open and close a connection for every request. So I try to keep a connection to handle every request like this.

public class MongoUtils {
private static final String connectionString = "mongodb://localhost:27017";
private static final MongoClient client;

static {
    client = MongoClients.create(connectionString);
}

public static MongoClient getConnection(){
    return client;
}

}

But maybe I'm doing this wrong somewhere. I don't know why it create the first connection and leave it there, then it create the second connection and use that to handle my db request. Here's the log

2018-10-25 11:37:36 INFO  AnnotationMBeanExporter:433 - Registering beans for JMX exposure on startup
2018-10-25 11:37:36 INFO  Http11NioProtocol:180 - Starting ProtocolHandler ["http-nio-8808"]
2018-10-25 11:37:36 INFO  NioSelectorPool:180 - Using a shared selector for servlet write/read
2018-10-25 11:37:36 INFO  TomcatWebServer:206 - Tomcat started on port(s): 8808 (http) with context path '/api'
2018-10-25 11:37:36 INFO  Backend:59 - Started Backend in 3.251 seconds (JVM running for 6.935)
2018-10-25 11:37:56 INFO  [/api]:180 - Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-10-25 11:37:56 INFO  DispatcherServlet:494 - FrameworkServlet 'dispatcherServlet': initialization started
2018-10-25 11:37:56 INFO  DispatcherServlet:509 - FrameworkServlet 'dispatcherServlet': initialization completed in 39 ms
2018-10-25 11:37:56 INFO  cluster:71 - Cluster created with settings {hosts=[10.184.153.232:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
2018-10-25 11:37:56 INFO  cluster:71 - Cluster description not yet available. Waiting for 30000 ms before timing out
2018-10-25 11:37:56 INFO  connection:71 - Opened connection [connectionId{localValue:1, serverValue:27}] to 10.184.153.232:27017
2018-10-25 11:37:56 INFO  cluster:71 - Monitor thread successfully connected to server with description ServerDescription{address=10.184.153.232:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 0, 3]}, minWireVersion=0, maxWireVersion=7, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=3393851}
2018-10-25 11:37:56 INFO  connection:71 - Opened connection [connectionId{localValue:2, serverValue:28}] to 10.184.153.232:27017
minhtus
  • 163
  • 1
  • 13

2 Answers2

3

Look up "connection pooling" which is the idea of having a "pool" of connections left open that your requests can use. If they are idle a while you can program them to close; conversely if they are overloaded you can program a connection pool to open even more connections to accommodate the load. They are tunable / configurable in other ways.

djechlin
  • 59,258
  • 35
  • 162
  • 290
  • Thank you. I did take a look at the mongoDB pooling idea and kinda understand the main idea. MongDB driver suggest having a singleton MongoClient instance and it will automatically handle the pooling for me. Looks like my solution is following the right way – minhtus Oct 25 '18 at 07:01
2

To quote directly from the JavaDocs of com.mongodb.MongoClient:

A MongoDB client with internal connection pooling. For most applications, you should have one MongoClient instance for the entire JVM.

The client itself has internal connection pooling and is thread-safe. So you should use a single MongoClient instance for your application. This suggests that you're already using it as recommended.

ernest_k
  • 44,416
  • 5
  • 53
  • 99
  • So according to that, I'm doing the right way. But I'm still wonder why it create 2 connections (3 last lines of the log I post) but only use the second connection for executing every db request. – minhtus Oct 25 '18 at 06:46
  • @mtu You aren't creating connections. Your code is using a `MongoClient` object, which creates and uses connections internally. A single client uses an internal connection pool, which may have more than one connection object. In other words, it's fine that you see different connections being created/made to the database, this is the internal connection pool growing as your application makes more requests. – ernest_k Oct 25 '18 at 06:52
  • Oh now I get it, I misunderstand between connection and client. I just thought they're the same. Thank you – minhtus Oct 25 '18 at 07:07