gpt4 book ai didi

java - 我该如何解决 MongoWaitQueueFullException?

转载 作者:可可西里 更新时间:2023-11-01 09:20:53 24 4
gpt4 key购买 nike

我运行一个 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)

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

最佳答案

您需要检查您在设置连接时给出的每个主机的连接值是多少(查看异常我认为您应该将其设置为 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

关于java - 我该如何解决 MongoWaitQueueFullException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25346951/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com