简体   繁体   English

我应该保持数据库连接打开以处理每个请求吗? 以及如何以正确的方式做到这一点

[英]Should I keep a database connection open to handle every request? And how to do that in the right way

For example a database such as MongoDB. 例如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

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. 它们可以其他方式进行可调/配置。

To quote directly from the JavaDocs of com.mongodb.MongoClient : 要直接引用com.mongodb.MongoClient的JavaDoc:

A MongoDB client with internal connection pooling. 具有内部连接池的MongoDB客户端。 For most applications, you should have one MongoClient instance for the entire JVM . 对于大多数应用程序, 整个JVM应该具有一个MongoClient实例

The client itself has internal connection pooling and is thread-safe. 客户端本身具有内部连接池,并且是线程安全的。 So you should use a single MongoClient instance for your application. 因此,您应该为应用程序使用一个MongoClient实例。 This suggests that you're already using it as recommended. 这表明您已经按照建议使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM