Solve the problem of mongodb transaction writeconflict



Recent attempts to use mongodb’s transaction feature in the project
Found a hole Command failed with error 112 (WriteConflict): 'WriteConflict'
When the same document is operated concurrently, the above prompt will appear
And then after retrying, there will be no


After querying the data, it is found that the above problem is caused by a parameter
This parameter is the transaction lock maximum wait event (MS)
The default value of this parameter is 5 (MS), which means that the prompt will be given when the lock is acquired beyond this timeWriteConflict


Method 1: use this to modify the value online

db.adminCommand( { setParameter: 1, maxTransactionLockRequestTimeoutMillis: 3000 } );

Mode 2: add parameters when starting

mongod --setParameter maxTransactionLockRequestTimeoutMillis=3000

Mode 3: in(/etc/mongod.cnf)Add the following configuration to

setParameter = maxTransactionLockRequestTimeoutMillis=3000

among3000It is the maximum lock waiting event, which can be freely deployed


After adding this parameter, you can wait for a while to see if there is anyWriteConflictTips
If you can, it is recommended to restart the application and Mongo

Recommended Today

Python basics Chinese series tutorial · translation completed

Original: Python basics Python tutorial Protocol: CC by-nc-sa 4.0 Welcome anyone to participate and improve: a person can go very fast, but a group of people can go further. Online reading Apache CN learning resources catalog introduce Seven reasons to learn Python Why Python is great Learn Python introduction Executing Python scripts variable character string […]