【问题标题】:MongoDB+Azure+Android: Error: com.mongodb.MongoException: not talking to master and retries used upMongoDB+Azure+Android: 错误: com.mongodb.MongoException: not talk to master and retries used up
【发布时间】:2013-12-24 14:25:22
【问题描述】:

背景

我有 MongoDB 副本集在 Azure 云服务工作者 + Web 角色中运行,使用 mongo-azure library。我第一次设置我的 Android 项目以连接到 MongoDB/Azure 时,IO(读取和写入)工作。然后,由于我的硬编码测试 JSONString 中的拼写错误,我使应用程序崩溃,因此 Mongo 实例无法正确关闭。修复 JSONString 问题后,我收到以下错误(在堆栈跟踪中)并且无法访问 MongoDB/Azure。也许没有正确关闭连接导致错误?

问题

这个错误到底是什么意思?为什么主实例不可用?

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)

注意事项

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

可能的解决方案

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

【问题讨论】:

  • 这是本系列的下一个 MongoDB+Azure+Android 问题:link

标签: java android mongodb azure mongodb-java


【解决方案1】:

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

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);

有关详细信息,请参阅source

【讨论】:

    猜你喜欢
    • 2023-03-10
    • 2013-12-25
    • 2022-12-27
    • 1970-01-01
    • 1970-01-01
    • 2014-03-06
    • 1970-01-01
    • 2014-07-28
    相关资源
    最近更新 更多