Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-4684

Read operations in transactions are incorrectly retried

      Summary

      The issue occurs when the first transactional read operation, which should initiate a transaction, fails (for instance, due to reelection of primary node).

      Mongo Driver incorrectly retries the operation, which leads to an error 251 "NoSuchTransaction".

      The expected result as per the retryable reads specification is that the read operation is not retried when executed in a transaction.

      Note that even after the bug is fixed, the outcome will be largely the same: an exception will be thrown with the TransientTransactionError error label. However, it will be a less confusing exception, and there will no longer be a retry which is contrary to specification. If the application uses ClientSession.withTransaction, the whole transaction will be retried upon catching an exception with the TransientTransactionError error label.

      Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

      org.mongodb:mongodb-driver-core:4.6.1

      We are using a replica set, managed by Atlas.

      How to Reproduce

      Run a find() command inside a transactional context after a reelection of primary node (in fact any network-specific error will do). 

      Example:

      First attempt:

      2022-07-22 10:46:26.664 DEBUG 1432 --- [nio-8080-exec-4] org.mongodb.driver.protocol.command      : 
      Sending command '\{"find": "submissions", "filter": {}, "projection": \{"census": false, "benefitPlan": false, "terms": false, "producer": false}, "limit": 20, "$db": "dev-submission-api", "$clusterTime": \{"clusterTime": {"$timestamp": {"t": 1658479556, "i": 1}}, "signature": \{"hash": {"$binary": {"base64": "DjgGnH/anK30OdlpOxrosXKL/U4=", "subType": "00"}}, "keyId": 7102362910864703492}}, "lsid": \{"id": {"$binary": {"base64": "jjddGYaCTNizTnGWaLqj+g==", "subType": "04"}}}, "txnNumber": 2, "startTransaction": true, "autocommit": false}' with request id 105 to database dev-submission-api on connection [connectionId\{localValue:45, serverValue:91713}] to server _________-shard-00-01.ezibq.mongodb.net:27017
      

      Then a connection error followed by a retry (note the missing startTransaction flag):

      2022-07-22 10:48:06.315 DEBUG 1432 --- [nio-8080-exec-4] org.mongodb.driver.protocol.command      : 
      Sending command '\{"find": "submissions", "filter": {}, "projection": \{"census": false, "benefitPlan": false, "terms": false, "producer": false}, "limit": 20, "$db": "dev-submission-api", "$clusterTime": \{"clusterTime": {"$timestamp": {"t": 1658479556, "i": 1}}, "signature": \{"hash": {"$binary": {"base64": "DjgGnH/anK30OdlpOxrosXKL/U4=", "subType": "00"}}, "keyId": 7102362910864703492}}, "lsid": \{"id": {"$binary": {"base64": "jjddGYaCTNizTnGWaLqj+g==", "subType": "04"}}}, "txnNumber": 2, "autocommit": false}' with request id 118 to database dev-submission-api on connection [connectionId\{localValue:73, serverValue:91747}] to server __________-shard-00-01.ezibq.mongodb.net:27017
      

      Followed by a confusing error message (but note the correct error label):

      com.mongodb.MongoCommandException: Command failed with error 251 (NoSuchTransaction): 
      'Given transaction number 2 does not match any in-progress transactions. 
      The active transaction number is 1' on server ____________-shard-00-01.ezibq.mongodb.net:27017. 
      The full response is \{"errorLabels": ["TransientTransactionError"], 
      "ok": 0.0, "errmsg": "Given transaction number 2 does not match any in-progress transactions. The active transaction number is 1", 
      "code": 251, "codeName": "NoSuchTransaction", 
      "$clusterTime": {"clusterTime": {"$timestamp": {"t": 1658479676, "i": 1}}, "signature": \{"hash": {"$binary": {"base64": "WpRsowuO7W+lJR+EGTVgZzL6rqw=", "subType": "00"}}, "keyId": 7102362910864703492}}, "operationTime": \{"$timestamp": {"t": 1658479676, "i": 1}}}
      

            Assignee:
            jeff.yemin@mongodb.com Jeffrey Yemin
            Reporter:
            vitalijs_levasins@epam.com Vitalijs Levasins
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: