非活动状态30分钟后,Mongo DB的套接字超时

在大约30分钟的不活动后,当我尝试插入到Mongo时,开始出现以下错误,当我再次尝试时,它开始工作。下面的错误。我在Azure上:

[INFO ] 2018-09-10T12:00:43,188 [http-nio-8080-exec-6] connection - Closed connection [connectionId{localValue:3, serverValue:26}] to XX.XX.XX.XX:27017 because there was a socket exception raised by this connection.
[ERROR] 2018-09-10T12:00:43,189 [http-nio-8080-exec-6] [dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.mongodb.UncategorizedMongoDbException: Timeout while receiving message; nested exception is com.mongodb.MongoSocketReadTimeoutException: Timeout while receiving message] with root cause
java.net.SocketTimeoutException: Read timed out
        at java.net.SocketInputStream.socketRead0(Native Method) ~[?:1.8.0_181]
        at java.net.SocketInputStream.socketRead(SocketInputStream.java:116) ~[?:1.8.0_181]
        ava.net.SocketTimeoutException: Read timed out
t java.net.SocketInputStream.read(SocketInputStream.java:171) ~[?:1.8.0_181]
        at java.net.SocketInputStream.read(SocketInputStream.java:141) ~[?:1.8.0_181]

我是如何初始化我的mongo模板的:

@Bean
    public MongoDbFactory mongoDbFactory() {
        String[] addresses = mongoUri.split(",");
        List<ServerAddress> servers = new ArrayList<>();
        for (String address : addresses) {
            String[] split = address.trim().split(":");
            servers.add(new ServerAddress(split[0].trim(), Integer.parseInt(split[1].trim())));
        }
        MongoClientOptions.Builder mongoOperations = MongoClientOptions.builder();
        mongoOperations.socketTimeout(1000 * 20); // I tried to increase the socket timeout to see if it helps but no help either
        mongoOperations.connectTimeout(1000 * 10);
        MongoClient mongoClient = new MongoClient(servers, MongoCredential.createCredential(userName, dbName, password.toCharArray()), mongoOperations.build());
        return new SimpleMongoDbFactory(mongoClient, dbName);
    }

    @Bean
    public MongoTemplate getMongoTemplate() {
        return new MongoTemplate(mongoDbFactory());
    }

我的魔神版本是3.6.4,我使用的是相同版本的Java驱动程序。 我尝试增加/减少文档中提供的tcp_keepalive_time设置 sudo sysctl -w net.ipv4.tcp_keepalive_time=120但也没有帮助。


解决方案

ohk。因此,我们发现mongo java driver jar比我们正在使用的导致此问题的mongo服务器更旧。确保驱动程序支持Mongo服务器的版本。

相关文章