writeConcern is not allowed within a multi-statement transaction with Java

Hi,

I am playing around with MongoDB got some errors.

I was trying to create the transaction, but getting an error similar to the post here.

This is how my code approximately looks like:

   var session = mongoClient.startSession();
        try {
           session.startTransaction();
           var database = client.getDatabase(databaseName);
           database.createCollection(session, "somename");
           addIndexes(session); // getting database same way
           session.commitTransaction();
        } catch (RuntimeException ex) {
            session.abortTransaction();
            throw ex;
        } finally {
            session.close();
        }

I have tried supplying all possible WriteConcerns as transaction options but getting same error.

Full exception message:

com.mongodb.MongoCommandException: Command failed with error 72 (InvalidOptions): 'writeConcern is not allowed within a multi-statement transaction' on server <server address>. The full response is {"operationTime": {"$timestamp": {"t": 1625923453, "i": 1}}, "ok": 0.0, "errmsg": "writeConcern is not allowed within a multi-statement transaction", "code": 72, "codeName": "InvalidOptions", "$clusterTime": {"clusterTime": {"$timestamp": {"t": 1625923453, "i": 1}}, "signature": {"hash": {"$binary": {"base64": "SEQ9iKC3UBcoyz5PuwNWVaSl93k=", "subType": "00"}}, "keyId": 6951399947191189506}}}
1 Like

I am having the same issue. It seems to occur when trying to perform an index operation within a transaction. Were you able to figure out a solution to this?

No, I still face the problem

Dang. I am trying to run this op on a MongoDB replica set running v4.4.8, which is supposed to support the creation of indexes within transactions. Very confused as to why we are getting this error.