Given transaction number does not match any in-progress transactions with Mongo Java

Hi Team,

We are frequently encountering the below error message when trying to transactionally update documents together:

Caused by: com.mongodb.MongoCommandException: Command failed with error 251 (NoSuchTransaction): 'Given transaction number 4 does not match any in-progress transactions. The active transaction number is 3' on server cluster0-shard-00-02.***** The full response is {"errorLabels": ["TransientTransactionError"], "ok": 0.0, "errmsg": "Given transaction number 4 does not match any in-progress transactions. The active transaction number is 3", "code": 251, "codeName": "NoSuchTransaction", "$clusterTime": {"clusterTime": {"$timestamp": {"t": 1682465400, "i": 14}}, "signature": {"hash": {"$binary": {"base64": "vqwdne/1kG8A2qNJ4nsqFsx+6V0=", "subType": "00"}}, "keyId": 7187829697743421442}}, "operationTime": {"$timestamp": {"t": 1682465400, "i": 14}}}

We are using mongodb java package org.mongodb:mongodb-driver-core:4.9.1

And the example code is as following:

    ClientSession clientSession = _mongoClient.startSession();
        TransactionOptions.builder().writeConcern(WriteConcern.MAJORITY).maxCommitTime(1L, TimeUnit.MINUTES).build());
    return /* A code block to async calling many _mongoCollection.updateOne(clientSession, filter, document) */
      .map(success -> {
        return (Void) null;
      }).onFailure(t -> {

Although we did not do a retry in the above logic, I am just wondering why it would hit an issue with transaction ID mismatch, given that the clientSession is passed in the mongo update?

Thank you!

ClientSession instances can’t be used concurrently like it appears you’re doing. Trying doing one updateOne at a time, or else batch them using the bulkWrite method.

it may help a bit if you van paste the code for this part and/or other related info. (indeed, the clientSession instance is not thread-safe, however without more info on request handling, hard to say if you use multi threading or not).

@Jeffrey_Yemin @Kobe_W Thank you for your reply!
Yeah, my code involves multi-threads to update the mongo data across multiple collections.

One more question: May I ask if I could synchronize the clientSession to make it working here?

Yes, but in that case it’s not really worth it to do anything asynchronously. Better to just loop in a single thread and not worry about synchronization. But do consider batching updates using bulkWrite. The latency savings of that can be substantial.

1 Like

Yeah make sense!! Thank you for your suggestions!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.