[使用Java的Mongo Connection池接收消息时超时

问题描述 投票:2回答:1

在生产中,我们正在使用Java与MongoDB连接(连接池)。每天我们都会收到将近500个带有以下错误的请求(MongoSocketReadTimeoutException),并且我们没有使用任何复杂的查询。可能是过时的Mongo连接吗?

我没有发现我们的代码或MongoDB速度慢的问题。请在下面查看我的代码,并建议是否需要添加任何参数或需要更改任何内容。

    at 2018-03-07 19:52:43 ERROR ::Error while connecting the Mongo DB {}
com.mongodb.MongoSocketReadTimeoutException: Timeout while receiving message
        at com.mongodb.connection.InternalStreamConnection.translateReadException(InternalStreamConnection.java:474)
        at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:225)
        at com.mongodb.connection.UsageTrackingInternalConnection.receiveMessage(UsageTrackingInternalConnection.java:102)
        at com.mongodb.connection.DefaultConnectionPool$PooledConnection.receiveMessage(DefaultConnectionPool.java:435)
        at com.mongodb.connection.CommandProtocol.execute(CommandProtocol.java:112)
        at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:159)
        at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:286)
        at com.mongodb.connection.DefaultServerConnection.command(DefaultServerConnection.java:173)
        at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:215)
        at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:206)
        at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:112)
        at com.mongodb.operation.FindOperation$1.call(FindOperation.java:487)
        at com.mongodb.operation.FindOperation$1.call(FindOperation.java:482)
        at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:239)
        at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:212)
        at com.mongodb.operation.FindOperation.execute(FindOperation.java:482)
        at com.mongodb.operation.FindOperation.execute(FindOperation.java:79)
        at com.mongodb.Mongo.execute(Mongo.java:772)
        at com.mongodb.Mongo$2.execute(Mongo.java:759)
        at com.mongodb.FindIterableImpl$FindOperationIterable.first(FindIterableImpl.java:207)
        at com.mongodb.FindIterableImpl.first(FindIterableImpl.java:148)
        at com.tecnotree.bom.validation.dao.MongoManager.getJsonObject(MongoManager.java:88)
        at com.tecnotree.bom.validation.dao.CustomerMasterDao.getService(CustomerMasterDao.java:48)
        at com.tecnotree.bom.validation.service.ValidationService.processValidation(ValidationService.java:214)
        at com.tecnotree.bom.validation.service.ValidationService.processRequest(ValidationService.java:125)
        at sun.reflect.GeneratedMethodAccessor185.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
        at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvid
er.java:205)

代码

public class ConnectionManager {
    private ConnectionManager() {

    }
    private static final Logger logger = LoggerFactory.getLogger(ConnectionManager.class);
    private static MongoClient mongoClient = null;


    private static String connections = null;
    private static String connectionTimeOut = null;
    private static String socketTimeOut = null;
    private static String serverSelectionTimeOut = null;
    private static String URL = null;
    private static String username = null;
    private static String password = null;
    private static String hostname = null;
    private static String port = null;
    private static String maxConnectionIdleTime = null;
    private static String minConnectionsPerHost = null;
    private static String maxConnectionLifeTime = null;



    private static String dbName = null;

    static {
        dbName = ValidationProperties.getValue("clm.db.dbName");
        mongoClient = getCLMSecondaryMongoConnection();


    }

    private static MongoClient getCLMSecondaryMongoConnection() {

        connections = ValidationProperties.getValue("clm.db.connectionsPerHost");
        connectionTimeOut = ValidationProperties.getValue("clm.db.connectTimeoutMS");
        socketTimeOut = ValidationProperties.getValue("clm.db.socketTimeOut");
        serverSelectionTimeOut = ValidationProperties.getValue("clm.db.serverSelectionTimeOut");
        username = ValidationProperties.getValue("clm.db.userName");
        password = ValidationProperties.getValue("clm.db.password");
        hostname = ValidationProperties.getValue("clm.db.hostname");
        port = ValidationProperties.getValue("clm.db.portnumber");
        maxConnectionIdleTime = ValidationProperties.getValue("clm.db.maxConnectionIdleTime");
        minConnectionsPerHost = ValidationProperties.getValue("clm.db.minConnectionsPerHost");

        try {
            List<MongoCredential> creds = new ArrayList<MongoCredential>();
            creds.add(MongoCredential.createCredential(username, dbName, password.toCharArray()));
            /*creds.add(MongoCredential.createMongoCRCredential(username, dbName, password.toCharArray()));*/
            MongoClientOptions.Builder optionsBuilder = MongoClientOptions.builder();
            optionsBuilder.connectTimeout(Integer.parseInt(connectionTimeOut));
            optionsBuilder.serverSelectionTimeout(Integer.parseInt(serverSelectionTimeOut));
            optionsBuilder.socketTimeout(Integer.parseInt(socketTimeOut));
            optionsBuilder.connectionsPerHost(Integer.parseInt(connections));
            optionsBuilder.maxConnectionIdleTime(Integer.parseInt(maxConnectionIdleTime));
            optionsBuilder.minConnectionsPerHost(Integer.parseInt(minConnectionsPerHost));
            if(Boolean.valueOf(ValidationProperties.getValue("clm.db.useSecondaryMongoForRead"))) {
                logger.info("Read is going for secondary mongoDB");
                optionsBuilder.readPreference(ReadPreference.secondaryPreferred());
            }


            MongoClientOptions options = optionsBuilder.build();
            mongoClient = new MongoClient(new ServerAddress(hostname, Integer.parseInt(port)), creds, options);

        } catch (Exception e) {
            logger.error("Error while connecting the Mongo DB {}", e);

        }
        return mongoClient;

    }



    public static MongoClient getMongoClient() {
        return mongoClient;
    }
java mongodb mongodb-query connection-pooling timeoutexception
1个回答
0
投票

在您的代码中,您正在设置各种MongoClientOptions,但是您没有说明正在使用什么值。最有可能是其中一种设置引起了问题。我的第一个猜测是socketTimeout太小,我的第二个猜测是connectionsPerHost小于您在生产中收到的并发请求的最大数目。

我假设您无法在生产环境中进行测试,因此,您应该做的第一件事就是尝试在测试环境中重现问题。您可以使用免费的JMeter tool将一定的负载重现到服务器。使用它的加速功能可以查看问题是否在达到一定负载后开始出现,还是随机出现。

如果您仍然无法找出问题的根源,也可以尝试注释掉所有的optionBuilder设置(这样您将使用所有默认设置)。如果这样可以解决问题,则可以一次重新设置一下配置选项,以查看哪个是有问题的。

© www.soinside.com 2019 - 2024. All rights reserved.