Java MongoDB+;Azure+;安卓:错误:com.mongodb.MongoException:未与主机对话,重试次数已用完

Java MongoDB+;Azure+;安卓:错误:com.mongodb.MongoException:未与主机对话,重试次数已用完,java,android,mongodb,azure,mongodb-java,Java,Android,Mongodb,Azure,Mongodb Java,背景: 我有在Azure云服务工作者+web角色中运行的MongoDB副本集,使用。我第一次将Android项目设置为连接到MongoDB/Azure时,IO(读写)工作正常。然后,由于硬编码测试JSONString中的一个输入错误,我导致应用程序崩溃,因此Mongo实例无法正确关闭。在修复了JSONString问题后,我收到了以下错误(在堆栈跟踪中),无法访问MongoDB/Azure。可能是未正确关闭连接导致了错误 问题: E/AndroidRuntime(6274): FATAL EXCE

背景

我有在Azure云服务工作者+web角色中运行的MongoDB副本集,使用。我第一次将Android项目设置为连接到MongoDB/Azure时,IO(读写)工作正常。然后,由于硬编码测试JSONString中的一个输入错误,我导致应用程序崩溃,因此Mongo实例无法正确关闭。在修复了JSONString问题后,我收到了以下错误(在堆栈跟踪中),无法访问MongoDB/Azure。可能是未正确关闭连接导致了错误

问题

E/AndroidRuntime(6274): FATAL EXCEPTION: Thread-7108
E/AndroidRuntime(6274): Process: com.myproject.examplemongodb, PID: 6274
E/AndroidRuntime(6274): com.mongodb.MongoException: not talking to master and retries used up
E/AndroidRuntime(6274):     at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:271)
E/AndroidRuntime(6274):     at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:273)
E/AndroidRuntime(6274):     at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:273)
E/AndroidRuntime(6274):     at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:216)
E/AndroidRuntime(6274):     at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:288)
E/AndroidRuntime(6274):     at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:273)
E/AndroidRuntime(6274):     at com.mongodb.DB.getCollectionNames(DB.java:400)
E/AndroidRuntime(6274):     at com.myproject.examplemongodb.ActivityMain$1.run(ActivityMain.java:89)
E/AndroidRuntime(6274):     at java.lang.Thread.run(Thread.java:841)
这个错误到底意味着什么?为什么主实例不可用

E/AndroidRuntime: com.mongodb.MongoException: not talking to master and retries used up
完整堆栈跟踪

E/AndroidRuntime(6274): FATAL EXCEPTION: Thread-7108
E/AndroidRuntime(6274): Process: com.myproject.examplemongodb, PID: 6274
E/AndroidRuntime(6274): com.mongodb.MongoException: not talking to master and retries used up
E/AndroidRuntime(6274):     at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:271)
E/AndroidRuntime(6274):     at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:273)
E/AndroidRuntime(6274):     at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:273)
E/AndroidRuntime(6274):     at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:216)
E/AndroidRuntime(6274):     at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:288)
E/AndroidRuntime(6274):     at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:273)
E/AndroidRuntime(6274):     at com.mongodb.DB.getCollectionNames(DB.java:400)
E/AndroidRuntime(6274):     at com.myproject.examplemongodb.ActivityMain$1.run(ActivityMain.java:89)
E/AndroidRuntime(6274):     at java.lang.Thread.run(Thread.java:841)
注释

  • 我正在使用最新版本(目前为2.11.3)
  • 我正在远程连接到数据库(非本地主机)
  • 另一位提到了这个错误,但OP使用了不同的驱动程序,他的解决方案不适用于Android/Java
可能的解决方案

  • 似乎关闭应用程序的连接可以使下一次运行正常运行。但是,我仍然担心能否允许并发连接,这是我绝对希望做到的。
    • 更新:我已经打包了一个mongodb登录和注销,每个查询都会转到服务器,但是错误似乎仍然会间歇性地出现。到目前为止,似乎每次都写作业,但一半时间只读作业。另一半时间是发布的错误

间歇性错误的原因是驱动程序的默认读取首选项,主要与副本集有关。默认的读取首选项是主。对于下面提到的每种模式,主数据库指的是主数据库(总是最新的),次数据库指的是从数据库,它们基本上是主数据库的副本,并不总是最新的

PRIMARY: The default read mode. Read from primary only. Throw an error if
         primary is unavailable. Cannot be combined with tags.
将读取首选项更改为以下选项之一的解决方案:

PRIMARY PREFERRED: Read from primary if available, otherwise a secondary.
SECONDARY PREFERRED: Read from a secondary if available, otherwise read from the primary.
NEAREST: Read from any member node from the set of nodes which respond the fastest.
示例代码:

// Use this when doing a read if you don't care if the data is always consistent.
// Change the following with secondaryPreferred() if you have high writes, so
// that you don't interfere with them.
ReadPreference preference = ReadPreference.primaryPreferred();
DBCursor cur = new DBCursor(collection, query, null, preference);

有关更多信息,请参阅。

以下是本系列中的下一个MongoDB+Azure+Android问题: