与MongoDB的TCP连接过多

时间:2018-10-15 13:12:46

标签: java mongodb scala morphia

我的项目使用play框架,并具有一个使用scala和singleton模式的MorphiaDBHelper。有一个使用Java并调用MorphiaDBHelper将信息保存到MongoDB的DBAccessor类。

我有2个问题:

  1. 根据代码,到MongoDB的TCP连接应该只有一个,但是我发现到MongoDB的TCP连接总是有100个左右。谁能告知问题是什么以及如何解决?

  2. MongoDB似乎仅接受大约100个TCP连接,而不是更多。是MongoDB中默认池大小的限制吗?

MorphiaDBHelper.scala

@Singleton
class MorphiaDBHelper(configuration: Configuration) {
val serverAddress = new ServerAddress(mongoip, mongoport)
val credential = MongoCredential.createCredential(mongouser, sesDB, mongopass.toCharArray)
val clientOptions = MongoClientOptions.builder.build
val mongoClient = new MongoClient(serverAddress, credential, clientOptions)
val datastore = morphia.createDatastore(mongoClient, sesDB)
}

DBAccessor.java

public class DBAccessor {
private static MorphiaDBHelper morphiaDBHelper = null;
public static void initMorphiaDBHelper(MorphiaDBHelper dbHelper) {
    if(morphiaDBHelper == null)
        morphiaDBHelper = dbHelper;
}

public static MorphiaDBHelper getMorphiaDBHelper() {
    return morphiaDBHelper;
}

public static <T> void savePojo(T pojo) {
    try {
        morphiaDBHelper.datastore().save(pojo);
    } catch (Exception e) {
        e.printStackTrace();
        play.Logger.error(e.getMessage());
    }
}
}

0 个答案:

没有答案