无法弄清楚为什么我一直得到com.mongodb.MongoSocketReadException:过早地到达流的末尾

时间:2018-04-20 08:21:15

标签: java mongodb tomcat

试图弄清楚为什么我会继续获得下面的例外情况。

这是我的代码:

CodecRegistry pojoCodecRegistry = fromRegistries(MongoClient.getDefaultCodecRegistry(), fromProviders(PojoCodecProvider.builder().automatic(true).build()));
MongoClientOptions.Builder options_builder = new MongoClientOptions.Builder();
options_builder.maxConnectionIdleTime(60000);
MongoClientURI uri = new MongoClientURI("mongodb+srv://USERNAME:MYPASSWORD@database-yyc55.mongodb.net/test\n", options_builder);
MongoClient mongoClient = new MongoClient(uri);
MongoDatabase database = mongoClient.getDatabase("test");
database = database.withCodecRegistry(pojoCodecRegistry);
MongoCollection<AppUser> collection = database.getCollection("users", AppUser.class);
AppUser appUser = null;
String userName = null;
appUser = collection.find().first();
userName = facebook.getName();

你可以看到我做了一些研究,并尝试将maxIdleConnection设置为60000毫秒,但仍然,我一直得到这个例外。 奇怪的是 - 这段代码以前工作过,在没有使用数据库的一天之后,它就开始发生了。

例外:

  

com.mongodb.MongoSocketReadException:过早地到达流的末尾       在com.mongodb.connection.SocketStream.read(SocketStream.java:87)       at com.mongodb.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:547)       在com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:418)       at com.mongodb.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:290)       在com.mongodb.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:255)       在com.mongodb.connection.CommandHelper.sendAndReceive(CommandHelper.java:84)       在com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:34)       at com.mongodb.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:91)       在com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:51)       在com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:127)       在com.mongodb.connection.DefaultServerMonitor $ ServerMonitorRunnable.run(DefaultServerMonitor.java:114)       在java.lang.Thread.run(Thread.java:748)

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

显然,问题是我的IP已经改变,而在服务器上,我不允许访问任何其他IP。 它解决了它,但我无法解释为什么我得到了:&#34; com.mongodb.MongoSocketReadException:过早地达到了流的结束&#34;例外。 我还在我的代码中添加了选项构建器,如上面的问题所示。