如题,在springboot中,配置mongoTemplate的writeconcern参数不起作用。使用已经把secondary用fsyncLock()锁住,在primary上用指令插入数据,w=2时会出现waiting for replication timed out提示,配置方法代码如下:
// Version 1: set statically // mongoTemplate.setWriteConcern(WriteConcern.MAJORITY); // Version 2: provide a WriteConcernResolver, which is called for _every_ MongoAction // which might degrade performance slightly (not texted) // and is very flexible to determine the value // mongoTemplate.setWriteConcernResolver(action-> WriteConcern.W3); // Version 3: // mongoTemplate.setWriteConcernResolver(new MyWriteConcernResolver()); mongoTemplate.setWriteResultChecking(WriteResultChecking.EXCEPTION);
非常感谢
ghost812 未选择答案
您好,3个成员,用的是PSA架构
PSA架构肯定有问题,因为writeConcern对应成员是数据节点,A只能投票,不能确认数据写入,此时S被锁住,那么writeConcern等于2,那么此时只有主节点可以确认,无法满足等于2,如果配置wtimeOut会超时,否则前端就一直等待影响性能。建议配置wtimeout参数避免不满足writeConcern。如果w超过1的情况下,通常不建议PSA架构。
您好,我只是因为在测试java中writeConcern参数是否有效才把s节点锁住,在指令中,从主节点中inser,w=2会失败,但是spring中w=2能成功执行
看下spring中writeConcern是否真的成功还是有超时设置,只是主库成功,你可以捕获writeConcern结果
补充一下配置方法:
@Bean
public MongoTemplate mongoTemplate(MongoDatabaseFactory factory, MongoConverter converter){
MongoTemplate mongoTemplate = new MongoTemplate(factory, converter);
log.warn(“Bean mongoTemplate”);
// Version 1: set statically
// mongoTemplate.setWriteConcern(WriteConcern.MAJORITY);
// Version 2: provide a WriteConcernResolver, which is called for _every_ MongoAction
// which might degrade performance slightly (not texted)
// and is very flexible to determine the value
// mongoTemplate.setWriteConcernResolver(action-> WriteConcern.W3);
// Version 3:
// mongoTemplate.setWriteConcernResolver(new MyWriteConcernResolver());
mongoTemplate.setWriteResultChecking(WriteResultChecking.EXCEPTION);
return mongoTemplate;
}