mongodbtransactionsreplicationdistributed-transactionsaws-documentdb

Amazon Document DB: How to Implement multi-document transactions


When using mongo template to start a session for transaction in document db cluster.

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

Throws error

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

Cluster URL do have replication enabled

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

Replication config:

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
        }
    }
}

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

Any information on how overcome this would be helpful. thanks in advance.

Docs:

Document-DB

Mongo DB


Solution

  • Your first link says:

    Amazon DocumentDB currently does not support multi-statement transactions.

    With regard to

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

    DocumentDB is not MongoDB.