【发布时间】:2017-06-27 16:34:49
【问题描述】:
我在 java 中使用连接池。只是想确保我正确使用它。
所以,这是我的带有 getDatabase 方法的 mongoconnection 类。
public class MongoConnection {
private static MongoConnection mongoConnection = null;
public static MongoConnection getInstance() {
if (mongoConnection == null) {
mongoConnection = new MongoConnection();
}
return mongoConnection;
}
private MongoClient mongoClient = null;
private MongoDatabase mongoDatabase = null;
private MongoConnection() {
mongoClient = new MongoClient("localhost");
mongoDatabase = mongoClient.getDatabase("test");
}
public MongoDatabase getDatabase() {
return mongoDatabase;
}
}
这里是使用它的代码 sn-p。
public void insertCustomer(document){
MongoCollection<Document> collection =
MongoConnection.getInstance().getDatabase().getCollection("customers");
collection.insertOne(document);
}
inserCustomer 方法被多次调用。
仅此而已。
【问题讨论】:
-
您好,欢迎来到 Stack Overflow,“以正确的方式使用它”……您是什么意思?请阅读如何创建Minimal, Complete, and Verifiable example 并花时间通过welcome tour 了解您在此处的方式(并获得您的第一个徽章)。
-
连接数也不会随着时间的推移而减少。 2017-06-27T22:09:39.682+0500 I - [conn9] 端接 127.0.0.1:57468(现在打开 5 个连接) 2017-06-27T22:09:39.682+0500 I - [conn8] 端接 127.0.0.1 :57467(现在打开 5 个连接)这些是关闭我的应用程序后仍然存在的 5 个连接。
标签: java mongodb connection-pooling