本文介紹了非活動(dòng)狀態(tài)30分鐘后,Mongo DB的套接字超時(shí)的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
在大約30分鐘的不活動(dòng)后,當(dāng)我嘗試插入到Mongo時(shí),開(kāi)始出現(xiàn)以下錯(cuò)誤,當(dāng)我再次嘗試時(shí),它開(kāi)始工作。下面的錯(cuò)誤。我在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驅(qū)動(dòng)程序。
我嘗試增加/減少文檔中提供的tcp_keepalive_time
設(shè)置
sudo sysctl -w net.ipv4.tcp_keepalive_time=120
但也沒(méi)有幫助。
推薦答案
ohk。因此,我們發(fā)現(xiàn)mongo java driver jar
比我們正在使用的導(dǎo)致此問(wèn)題的mongo服務(wù)器更舊。確保驅(qū)動(dòng)程序支持Mongo服務(wù)器的版本。
這篇關(guān)于非活動(dòng)狀態(tài)30分鐘后,Mongo DB的套接字超時(shí)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,