我已通读https://docs.mongodb.com/manual/core/retryable-writes/并设置了所有必需的属性以使其正常运行,但到目前为止还算不上成功。
具有3个成员副本,并且程序正在运行以插入记录时,我杀死了主数据库,而在为新的主数据库进行选举时,我的代码中正在发生循环,并且当新成员被选举后,这些记录不会重试写入。
``
mongo --version
MongoDB shell version v3.6.6
git version: 6405d65b1d6432e138b44c13085d0c2fe235d6bd
OpenSSL version: OpenSSL 1.1.0h 27 Mar 2018
allocator: system
modules: enterprise
build environment:
distarch: x86_64
target_arch: x86_64
``
``compile group: 'org.mongodb', name: 'mongodb-driver', version: '3.6.4'
compile group: 'org.mongodb', name: 'mongodb-driver-core', version: '3.6.4'
compile group: 'org.mongodb', name: 'bson', version: '3.6.4'``
``dbUri = "mongodb://localhost:28000,localhost:28001,localhost:28002/test?replicaSet=rs01&retryWrites=true&w=2";``
MongoClient mongoClient = new MongoClient(new MongoClientURI(dbUri));
``
private static void insertData(MongoClient mongoClient) {
MongoDatabase db = mongoClient.getDatabase("test");
MongoCollection<Document> collection = db.getCollection("customer");
// collection.withWriteConcern(WriteConcern.W2);
deleteOldDocumets(collection);
int customerId = 0;
while (customerId < 1000) {
++customerId;
try {
Document document = new Document();
document.put("name", "user" + customerId);
document.put("age", customerId);
collection.insertOne(document);
logger.info("Customer : " + customerId + " Time is : "
+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")));
Thread.sleep(10);
} catch (Exception e) {
logger.info("Primary connection closed, starting election in 5 secs");
}
}
mongoClient.close();
}``
### DB master props ###
``MongoDB Enterprise rs01:PRIMARY> db.isMaster();
{
"hosts" : [
"localhost:28000",
"localhost:28001",
"localhost:28002"
],
"setName" : "rs01",
"setVersion" : 1,
"ismaster" : true,
"secondary" : false,
"primary" : "localhost:28000",
"me" : "localhost:28000",
"electionId" : ObjectId("7fffffff0000000000000001"),
"lastWrite" : {
"opTime" : {
"ts" : Timestamp(1540647424, 1),
"t" : NumberLong(1)
},
"lastWriteDate" : ISODate("2018-10-27T13:37:04Z"),
"majorityOpTime" : {
"ts" : Timestamp(1540647424, 1),
"t" : NumberLong(1)
},
"majorityWriteDate" : ISODate("2018-10-27T13:37:04Z")
},
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 100000,
"localTime" : ISODate("2018-10-27T13:37:10.713Z"),
"logicalSessionTimeoutMinutes" : 30,
"minWireVersion" : 0,
"maxWireVersion" : 6,
"readOnly" : false,
"ok" : 1,
"operationTime" : Timestamp(1540647424, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1540647424, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
``
答案 0 :(得分:0)
我在Java驱动程序3.6.3中遇到了同样的问题,而我的解决方案是将驱动程序版本升级到3.8.2。请同时检查此错误修复: https://jira.mongodb.org/browse/JAVA-2859?jql=project%20%3D%20JAVA%20AND%20issuetype%20%3D%20Bug%20AND%20fixVersion%20%3D%203.7.1%20ORDER%20BY%20priority%20DESC