1
私はmongo-java-driver-3.0.0ライブラリを使用して、MongoDBのインスタンスで多くのデータの挿入を進めています。MongoDBをJavaで複数のスレッドで正しく使用する方法は?
INFORMAÇÕES: Closed connection [connectionId{localValue:816}] to
localhost:27017 because the pool has been closed. Exception in thread
"user_4" com.mongodb.MongoSocketReadException:
Prematurely reached end of stream
私はインスタンスを呼び出して、挿入を進めていたコードは次のとおりです:
問題は、私は複数のスレッド(複数のユーザーをシミュレート)を使用しようとすると、いくつかの挿入後に次のエラーが発生したということですpublic static FachadaMongo getInstancia() {
if (instancia == null) {
instancia = new FachadaMongo();
}
return instancia;
}
public MongoDatabase getDB(String HOST, String PORT, String DB_NAME) {
MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
//build the connection options
builder.maxConnectionIdleTime(60000);//set the max wait time in (ms) 60 segundos
MongoClientOptions opts = builder.build();
int port = Integer.parseInt(PORT);
MongoClient mongoClient = new MongoClient(new ServerAddress(HOST, port), opts);
MongoDatabase db = mongoClient.getDatabase(DB_NAME);
return db;
}
public MongoCollection getColecao(String HOST, String PORT, String DB_NAME, String colecao) {
MongoCollection col = FachadaMongo.getInstancia().getDB(HOST, PORT, DB_NAME).getCollection(colecao);
return col;
}
public void insert(String HOST, String PORT, String DB_NAME, Document documento) {
this.getColecao(HOST, PORT, DB_NAME, "documentos").insertOne(documento);
}
このコードを1つのスレッドで使用しても、しばらくしても同じエラーが表示されます。複数のスレッドでは、エラーがより速くなります。 誰かが私を助けてくれたら、私は感謝しています。大学での最終的な仕事はそれに依存しています。
はい、閉鎖接続とストリームの終了に関する多くのトラブルシューティングを探しました。しかし、私が試した解決策のどれもは機能しません。 –