30分钟不活动后,mongo DB的套接字超时

时间:2018-09-10 12:21:44

标签: java mongodb azure spring-boot spring-data-mongodb

闲置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());
    }

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

1 个答案:

答案 0 :(得分:0)

哦。因此我们发现mongo java driver jar早于我们使用的mongo服务器,这导致了此问题。确保驱动程序支持mongo服务器的版本。