Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem on multiple updates on same document in Multi-Doc Transactions of MongoDB

I'm facing lot of troubles in using the multi-document transactions. One of which happens to be - Whenever , there are multiple updates on the same document [in the same transaction], Mongo throws a weird error "com.mongodb.MongoCommandException: Command failed with error 251 (NoSuchTransaction): 'Transaction 1 has been aborted.' on server .....". I took it in the direction of visibility and adjusted the options for Read and write concerns like

WriteConcern.MAJORITY , ReadPreference.PRIMARY , ReadConcern.SNAPSHOT.

I'm using the java driver and the snippet for transaction start is something like below

TransactionOptions transactionOptions = TransactionOptions.builder()
.writeConcern(WriteConcern.MAJORITY)
.readPreference(ReadPreference.primary())
.readConcern(ReadConcern.SNAPSHOT)
.build();

ClientSessionOptions clientSessionOptions = ClientSessionOptions
.builder()
.defaultTransactionOptions(transactionOptions)
.build();

this.session = mongo.startSession(clientSessionOptions);
this.session.startTransaction(transactionOptions);

Any one has an idea on this error?

like image 399
Kris Avatar asked Oct 14 '25 07:10

Kris


2 Answers

In order to get more context about the error, you need to check the logs of Mongodb.

In Mongo shell mongo, run show log global and you will see why the transaction has failed.

In my case, it was failing for this reason:

[startPeriodicThreadToAbortExpiredTransactions] Aborting transaction with 
txnNumber 1 on session ea5e8c8d-ecc2-47ca-8901-55a43143a809 because it has 
been running for longer than 'transactionLifetimeLimitSeconds'

This looks like a bug in MongoDB since it should throw atransactionLifetimeLimitSeconds error instead, or at least tell you what's the problem.

like image 165
Abdellah Alaoui Avatar answered Oct 17 '25 05:10

Abdellah Alaoui


The transaction might include an operation that modifies the database catalog. I encountered the same error caused due to an insert operation on a non-existent collection. The 251 error happens when committing the transaction.

my-rs:PRIMARY> use mytest
switched to db mytest
my-rs:PRIMARY> show collections
my-rs:PRIMARY> session.startTransaction()
my-rs:PRIMARY> session.getDatabase('mytest').newcollection.insert({"a":"b"});
WriteCommandError({
    "operationTime" : Timestamp(1554822412, 1),
    "ok" : 0,
    "errmsg" : "Cannot create namespace mytest.newcollection in multi-document transaction.",
    "code" : 263,
    "codeName" : "OperationNotSupportedInTransaction",
    "$clusterTime" : {
        "clusterTime" : Timestamp(1554822412, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
})
my-rs:PRIMARY> session.commitTransaction()
2019-04-09T15:07:04.695+0000 E QUERY    [js] Error: command failed: {
    "errorLabels" : [
        "TransientTransactionError"
    ],
    "operationTime" : Timestamp(1554822422, 1),
    "ok" : 0,
    "errmsg" : "Transaction 8 has been aborted.",
    "code" : 251,
    "codeName" : "NoSuchTransaction",
    "$clusterTime" : {
        "clusterTime" : Timestamp(1554822422, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:18:14
_assertCommandWorked@src/mongo/shell/assert.js:536:17
assert.commandWorked@src/mongo/shell/assert.js:620:16
commitTransaction@src/mongo/shell/session.js:942:17
@(shell):1:1
my-rs:PRIMARY>

Please refer to https://docs.mongodb.com/manual/core/transactions/#restricted-operations

like image 45
Sangwoo Lee Avatar answered Oct 17 '25 04:10

Sangwoo Lee