Amazon Document DB:如何实现多文档交易

Amazon Document DB: How to Implement multi-document transactions

当使用 mongo 模板为文档数据库集群中的事务启动会话时。

public class DocDBService() {
@Autowired
MongoTemplate mongoTemplate;

@Autowired
MongoTransactionManager mongoTransactionManager;

public void writeAsTransaction() {
  mongoTemplate.setSessionSynchronization(SessionSynchronization.ALWAYS);
  TransactionTemplate transactionTemplate = new TransactionTemplate(mongoTransactionManager);
  transactionTemplate.execute(
      new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(final TransactionStatus status) {
            mongoTemplate.save(new Document("abc", 1), "test");
            mongoTemplate.remove(new Document("abc", 1), "test");
        }
      }
  );
}
}

抛出错误

Sessions are not supported by the MongoDB cluster to which this client is connected

群集 URL 确实启用了 复制

mongodb://user:password@cluster-url/default?ssl=true&replicaSet=rs0&readPreference=secondaryPreferred

复制配置:

rs0:PRIMARY> rs.conf()
{
    "_id" : "rs0",
    "configsvr" : false,
    "protocolVersion" : 1,
    "writeConcernMajorityJournalDefault" : true,
    "members" : [
        {
            "_id" : 0,
            "host" : "cluster.****.us-west-*.docdb.amazonaws.com:27017",
            "arbiterOnly" : false,
            "buildIndexes" : true,
            "hidden" : false,
            "priority" : 1,
            "slaveDelay" : 0
        },
        {
            "_id" : 1,
            "host" : "cluster-1.****.us-west-*.docdb.amazonaws.com:27017",
            "arbiterOnly" : false,
            "buildIndexes" : true,
            "hidden" : false,
            "priority" : 1,
            "slaveDelay" : 0
        },
        {
            "_id" : 2,
            "host" : "cluster-2.****.us-west-*.docdb.amazonaws.com:27017",
            "arbiterOnly" : false,
            "buildIndexes" : true,
            "hidden" : false,
            "priority" : 1,
            "slaveDelay" : 0
        }
    ],
    "settings" : {
        "getLastErrorDefaults" : {
            "w" : "majority",
            "wtimeout" : 0
        }
    }
}

同样适用于本地 Mongo DB 3.6 单机复制。

有关如何克服此问题的任何信息都会有所帮助。提前致谢。

文档:

Document-DB

Mongo DB

你的 first link 说:

Amazon DocumentDB currently does not support multi-statement transactions.

关于

The same works in local Mongo DB 3.6 with a single machine replication.

DocumentDB 不是 MongoDB。

2020 年 11 月,Amazon DocumentDB 4.0 添加了对事务的支持。