mongoDB 常见三种异常解析 (附配置)

时间:2025-04-10 07:33:11

配置

mongoDB配置

<mongo:mongo-client id="mongoClient" host="${}" port="${}" credentials="${}:${}@${}">
        <mongo:client-options
            connections-per-host="${}"
            min-connections-per-host="${}"
            threads-allowed-to-block-for-connection-multiplier="${}"
            connect-timeout="${}"
            max-wait-time="${}"
            socket-keep-alive="${}"
            socket-timeout="${}"
            description="${}"
            max-connection-idle-time="${}"
            max-connection-life-time="${}"
            heartbeat-socket-timeout="${}"
            heartbeat-connect-timeout="${}"
            min-heartbeat-frequency="${}"
            heartbeat-frequency="${}" />
    </mongo:mongo-client>
mongo.host=127.0.0.1
mongo.port=27017
mongo.defaultDbName=data
mongo.user=test
mongo.pwd=123456

# 最大连接数,每一台服务器
mongo.connectionsPerHost=1
# 可阻塞线程队列容量
mongo.threadsAllowedToBlockForConnectionMultiplier=5
# 每一台服务器的最小连接数
mongo.minConnectionsPerHost=1

#连接超时时间 1分钟
mongo.connectTimeout=60000
#等待时间 120000 2 * 60 * 1000
mongo.maxWaitTime=120000
#Socket超时时间
mongo.socketTimeout=0
#保持连接
mongo.socketKeepAlive=true

mongo.description=mongodb

## 连接空闲时间8小时,否则连接太过频繁
mongo.maxConnectionIdleTime=28800000
mongo.maxConnectionLifeTime=0

#mongo 心跳
mongo.heartbeatSocketTimeout=10000
mongo.heartbeatConnectTimeout=15000
mongo.minHeartbeatFrequency=5000
mongo.heartbeatFrequency=100000

异常1 MongoWaitQueueFullException

请求线程过多时,阻塞的线程放入不到队列之中,

线程数量 - connectionsPerHost >  threadsAllowedToBlockForConnectionMultiplier * connectionsPerHost


线程数量 = connectionsPerHost * (threadsAllowedToBlockForConnectionMultiplier + 1)

根据以上公式可以得出
增大threadsAllowedToBlockForConnectionMultiplier 或者 connectionsPerHost 即可

通过多线程模拟即可

: Too many threads are already waiting for a connection. Max number of threads (maxWaitQueueSize) of 5 has been exceeded.
    at (:274)
    at (:93)
    at (:86)
    at (:77)
    at $(:86)
    at (:422)
    at (:415)
    at (:133)
    at (:60)
    at (:819)
    at $2.execute(:802)
    at (:340)
    at (:335)
    at (:326)
    at (:296)
    at (:262)
    at (:199)
    at $9.doInCollection(:1054)
    at (:482)
    ... 22 more

异常2 MongoTimeoutException

阻塞队列的线程等待连接超时

解决方案:: 增大maxWaitTime参数

复现方案: 通过多线程、大数据量,无索引查询。调小maxWaitTime测试即可

Caused by: : Timeout waiting for a pooled item after 5000 MILLISECONDS
    at (:127)
    at (:256)
    at (:97)
    at (:86)
    at (:77)
    at $(:86)
    at (:431)
    at (:406)
    at (:709)
    at (:81)
    at (:810)
    at $2.execute(:797)
    at (:871)
    at (:142)
    at (:680)
    at (:831)
    at (:794)
    at (:741)
    at $(:2197)
    at $(:2181)
    at (:1925)
    ... 18 more

异常3 MongoInterruptedException

由于线程被中断导致mongo查询被中断

查询中断异常

Caused by: : Interrupted acquiring a permit to retrieve an item from the pool 
    at (:186)
    at (:126)
    at (:256)
    at (:97)
    at (:86)
    at (:77)
    at $(:86)
    at (:422)
    at (:397)
    at (:232)
    at (:55)
    at (:810)
    at $2.execute(:797)
    at (:1004)
    at (:854)
    at $8.doInCollection(:779)
    at $8.doInCollection(:777)
    at (:482)
    ... 33 common frames omitted
Caused by: : null
    at (:1326)
    at (:409)
    at (:180)
    ... 50 common frames omitted

复现程序

    final Thread t1 = new Thread(() -> {
            (().toString());
            (().toString());
            (().toString());
            (().toString());
            (().toString());
            (().toString());
        });

        ();

        (10000);

        ();

        while (true) {

        }