Get the App
SLTechnology News&Howtos  ›  Database  › 

How to implement transaction rollback in mongoDB 4. 0

Shulou Source: shulou.com Published: 2022-05-31 21:11:03 11月08日 Update

This article shows you how to achieve transaction rollback in mongoDB 4.0. the content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

1. Preparatory work

Upgrade mongodb to 4.0.0

$brew upgrade mongodb

Upgrade or install mongodb.js v3.1.0 or above

$npm i mongodb-save-dev

Pit

After upgrading the above, when connecting mongo in db.js

Will warn you to add a field to the option of connect

UseNewUrlParser:true

At this time, if there is user authentication, you need to add another field to the option of connect

AuthSource: the db in which the user resides, usually admin

Otherwise, the verification failure will be reported and the user's error will not be found, such as

Const mongoClient = await MongoClient.connect (mongoClientUrl, {auth: {user: config.dbUserName, password: config.dbUserPassword,}, authSource:'admin', useNewUrlParser:true,})

two。 Transform an existing database into a replication set

Currently, transaction rollback can only be operated on replication sets, and mongodb server alone cannot operate transactions.

Turn off all mongod

Add-- replSet rs0 after the command to start mongod, such as

Mongod-dbpath. / db-- port 27017-- replSet rs0

Open a shell and create a mongo instance with different ports, such as

Mongod-dbpath. / db_repl-- port 27018-- replSet rs0

Connect to the mongo instance of 27017 and set the

$mongo$ rs.initiate () $rs.add ('localhost:27018')

Complete

3. Write rollback code

Added a new method in db.js

Export const getSession = async function () {return await state.mongoClient.startSession ();}

Call this method each time before the mongo code that needs to be rolled back to get the session, the tag to start the rollback

Const session = await db.getSession (); session.startTransaction ({readConcern: {level: 'snapshot'}, writeConcern: {w:' majority'},})

Every time you call the method of mongodb.js to operate on the database, you should bring session with you, such as

Db .collection (this.collecitonName) .insertOne (doc, {session})

After you deal with the error and feel the need to roll back, execute

Await session.abortTransaction ()

When you think it's okay, when it ends normally, execute.

Await session.commitTransaction ()

There may be some encapsulated code in my code that is not put on it, so it may not be understood. I just give a chestnut to implement, the specific code implementation can see reference link 1

4. Summary

The attribute "useNewUrlParser" will identify the db needed to authenticate users in url. You do not need to specify it before upgrading, so you must specify it to upgrade, whether it is behind url or using authSource.

Transaction rollback can only be operated on the replication set. I guess the principle of implementation may be like this: first record the session of the primary node, and then roll back, find the data snapshot of the secondary node through this session, and then apply the snapshot to the primary node to achieve rollback. Of course, the actual situation should be quite complex, otherwise mongoDB would not have taken 3 years to implement this operation.

The above is how to implement transaction rollback in mongoDB 4.0. have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Transactions code upgrades users data methods nodes validation content fields instances snapshots skills databases knowledge errors differences complexity conciseness brevity Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Docker Linux MySQL OPPO Reno