Recently, I encountered an online bug. Accessing a specific interface will cause occasional exceptions. After checking the log, it is found that an error is reported during the operation of mongodb. The error information is as follows:
error message: Command failed with error 251 (NoSuchTransaction): 'Given transaction number 115 does not match any in-progress transactions. The active transaction number is 114' on server xx.xx.xx.xx:xxxx. The full response is {"errorLabels": ["TransientTransactionError"], "ok": 0.0, "errmsg": "Given transaction number 115 does not match any in-progress transactions. The active transaction number is 114", "code": 251, "codeName": "NoSuchTransaction"}
Through searching and troubleshooting, the problem was located to be due to processing MongoDB operations where two requests in the same transaction are sent to the DB at the same time, with the probability of generating the following scenario.
1. request 1 and request 2 are sent to Mongo at the same time and start execution
2. Request 1 is still executing and request 2 has completed
3. Since request 1 is not yet completed, the transaction has not really started at DB level, so request 2 cannot end normally (that is why the error states that transaction id 115 cannot be found, because the transaction is not yet registered in DB), resulting in transaction rollback and throwing exceptions
4. request 1 execution is complete, but the transaction has been rolled back, the operation is invalid
Reviewing the code, we found that the reason why two requests are sent to DB at the same time is that the zipWith() method is used for data merging. The feature of this method is that it will request two data to be merged to the database at the same time, which will trigger the aforementioned problem when operating Mongo.
The solution is very simple, just use the zipWhen() method instead. zipWhen will block and wait for the first data requested to arrive before requesting the second data, which perfectly circumvents this problem.
Read More:
- Asynchronous processing of HTTP request by Java_ Method 1: through callable
- Spring boot uses thread pool to realize asynchronous processing without return and asynchronous processing with return
- Spring Error: Transaction synchronization is not active
- [Solved] In case of “transactionmanager” while setting bean property “transactionmanager” error
- Asynchronous processing of HTTP request by Java_ Method 2: through deferredresult
- [Solved] java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed
- [Solved] Feign Call Port Error: HttpMessageNotReadableException:JSON parse error:Illegal character ((CTRL-CHAR, code 31)
- Hystrix fuse of spring cloud system
- Solve the use of declaration transactions in spring java.lang.NoClassDefFoundError: org/aspectj/util/PartialOrder$PartialComparable
- [Solved] Sharding-Proxy Insert Datas Error: Sharding value must implements Comparable NoSuchElementException
- [Solved] Android project Compile error: error processing kotlin-stdlib-1.6.0.jar
- feign.codec.EncodeException: Type definition error: [simple type, class java.util.Collections$3]
- [Solved] mybatis Error querying database. Cause: java.sql.SQLException: The server time zone value
- [Solved] IO exception: NL exception was generated
- After asynchronous file import and springboot multipartfile upload, the @async asynchronous processing reports an error: nosuchfileexception
- Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource could [Solved]
- How to Solve zuul Forwarding error (No Retrayable)
- [Solved] Hibernate Error: Row was updated or deleted by another transaction
- The number of control threads and concurrency number of concurrent executor of Java starting gun
- Client Error: Could not get a resource from the pool [How to Solve]