我该如何解决MongoWaitQueueFullException?

时间:2014-08-17 06:54:15

标签: java multithreading mongodb threadpool mongodb-query

我运行一个java程序,它是一个线程执行程序,它将数千个文档插入到mongodb中的表中。我收到以下错误

Exception in thread "pool-1-thread-301" com.mongodb.MongoWaitQueueFullException: Too many threads are already waiting for a connection. Max number of threads (maxWaitQueueSize) of 500 has been exceeded.
    at com.mongodb.PooledConnectionProvider.get(PooledConnectionProvider.java:70)
    at com.mongodb.DefaultServer.getConnection(DefaultServer.java:73)
    at com.mongodb.BaseCluster$WrappedServer.getConnection(BaseCluster.java:221)
    at com.mongodb.DBTCPConnector$MyPort.getConnection(DBTCPConnector.java:508)
    at com.mongodb.DBTCPConnector$MyPort.get(DBTCPConnector.java:456)
    at com.mongodb.DBTCPConnector.getPrimaryPort(DBTCPConnector.java:414)
    at com.mongodb.DBCollectionImpl.insert(DBCollectionImpl.java:176)
    at com.mongodb.DBCollectionImpl.insert(DBCollectionImpl.java:159)
    at com.mongodb.DBCollection.insert(DBCollection.java:93)
    at com.mongodb.DBCollection.insert(DBCollection.java:78)
    at com.mongodb.DBCollection.insert(DBCollection.java:120)
    at ScrapResults103$MyRunnable.run(MyProgram.java:368)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    at java.lang.Thread.run(Thread.java:695)

我该如何解决这个问题?请帮我。

3 个答案:

答案 0 :(得分:5)

您需要检查在设置连接时您给出的每个主机值的连接数(查看我认为您将其设置为500的异常)。

MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
builder.connectionsPerHost(200);
MongoClientOptions options = builder.build();
mongoClient = new MongoClient(URI, connectionOptions);

设置每个主机的连接的理想方法是通过反复试验,但是您需要确保您设置的值不应超过打开mongo shell并执行的连接数:

  

db.serverStatus()。connections.available

答案 1 :(得分:3)

您处于maxWaitQueueSize限制,因此增加倍数;)

 MongoClientOptions options = MongoClientOptions.builder()
                .threadsAllowedToBlockForConnectionMultiplier(10)
                .build();

 MongoClient mongo = new MongoClient("127.0.0.1:27017", options);
 //run 2000 threads and use database ;)

答案 2 :(得分:2)

waitQueueMultiplemaxConnectionPoolSizethreadsAllowedToBlockForConnectionMultiplier的乘积,因此您可以修改这三个选项之一,以使用相应的值在MongoClientOptions 中调整您的应用并将其消费到您的MongoClient作为论证,上面的操作(标记为答案)https://stackoverflow.com/a/25347310/2852528

但是

我强烈建议您首先分析您的代码(与数据库通信),如果没有优化可用(例如缓存,使用聚合,分页等),请继续进行操作并更改选项