1636年插入后MongoDB抛出超时异常

时间:2018-08-19 19:46:55

标签: java mongodb exception timeout

所以我在Mongo中遇到了这个问题:我有一个Java程序,可将文档插入到Mongo中,数据是从名为“ Article”的Java对象中提取的,这是它的结构简化了,没有getter,setter,equals等:

public class Article {

    private String title;
    private String doi;
    private String country;
    private String date;
    private ArrayList<String> authors;
    private ArrayList<String> references;
}

我用于插入的代码是:

public static void saveArticle(Article article) {

    MongoClient mongoClient = new MongoClient("localhost",27017);
    MongoDatabase database = mongoClient.getDatabase("bibliometricDB");
    MongoCollection<Document> collection = database.getCollection("papers");        

    Document doc = new Document("doi",article.getDoi())
        .append("date", article.getDate())
        .append("title", article.getTitle())
        .append("country", article.getCountry())
        .append("authors", article.getAuthors())
        .append("references", article.getReferences());
        collection.insertOne(doc);
    }
}

我的 main 方法遍历大量文章(超过100.000),并为每个文章称其为“ saveArticle(Article)”。文章的数据是正确的(因为我之前检查过每篇文章),否则,该文章将被丢弃。

奇怪的地方:每次我运行此程序并开始处理文章时,插入编号1637到来时,Mongo都会引发以下错误(无论文章如何):

Exception in thread "main" com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadException: Prematurely reached end of stream}}]
    at com.mongodb.internal.connection.BaseCluster.getDescription(BaseCluster.java:179)
    at com.mongodb.internal.connection.SingleServerCluster.getDescription(SingleServerCluster.java:41)
    at com.mongodb.client.internal.MongoClientDelegate.getConnectedClusterDescription(MongoClientDelegate.java:136)
    at com.mongodb.client.internal.MongoClientDelegate.createClientSession(MongoClientDelegate.java:94)
    at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.getClientSession(MongoClientDelegate.java:249)
    at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:172)
    at com.mongodb.client.internal.FindIterableImpl.first(FindIterableImpl.java:198)

我真的被这个错误困扰。任何帮助,将不胜感激。 谢谢!

0 个答案:

没有答案