{ console.log("transaction star" /> { console.log("transaction star" /> { console.log("transaction star"/>

the transactions are not rolling back in mongodb

71 Views Asked by At
var mongo = require("mongoose")
session10 = await mongo.startSession
 try {
        
        await session10.withTransaction(async () => {
            console.log("transaction started");
        await readonlymodel.findOneAndUpdate({}, {
            $set: {
                readonlyMode: true,
                yearEndClosure: true
            }
        }, { session:session10 });
        var archivalActivity = {
            activity_id: 4,
            activityName: 'Current year Data Back',
            activity_Status: 'In Progress',
            err_occured: 'None',
            remarks: 'None'
        }
        await year_end_closure_model.findOneAndUpdate({ year: MCpayload.year, month: MCpayload.month, 'activities.activity_id': 3 }, {
            $set: {
                'activities.$.activity_Status': 'Completed',
            },
    
        }, { session:session10 });
        await year_end_closure_model.findOneAndUpdate({ year: MCpayload.year, month: MCpayload.month }, {
            $push: {
                activities: archivalActivity
            }
        }, {session: session10 })
 const dataCurrentYear = await model.find({}, { _id: 0 }, {session: session10 })
        const etBackupData = await archival_yearEnd.insertMany(etdataCurrentYear, {session: session10 });
        console.log(dataCurrentYear.length, etBackupData.length, "inserted Succesfully")
        const etproofCurrentYear = await proofmodel.find({}, { _id: 0 }, { session: session10 });
        const proofBackupData = await proof_archival_yearEnd.insertMany(proofCurrentYear, { session:session10 });
        console.log(proofCurrentYear.length, proofBackupData.length, "inserted Succesfully")
        const proofCurrentYear = await commonproofmodel.find({}, { _id: 0 }, {session: session10 });
        const proofBackupData = await common_proof_archival_yearEnd.insertMany(proofCurrentYear, { session:session10 });
        console.log(cmnproofCurrentYear.length, cmnproofBackupData.length, "inserted Succesfully")
        const etcaseCurrentYear = await etcasemodel.find({}, { _id: 0 }, {session: session10 });
        const etcaseBackupData = await caseStudy_proof_archival_yearEnd.insertMany(etcaseCurrentYear, {session: session10 });
        console.log(etcaseCurrentYear.length, etcaseBackupData.length, "inserted Succesfully")
        const SummaryCurrentYear = await summary_model.find({}, { _id: 0 }, {session: session10 });
        const SummaryBackupData = await Summary_archive_yearEnd.insertMany(SummaryCurrentYear, {session: session10 });
        console.log(SummaryCurrentYear.length, SummaryBackupData.length, "inserted Succesfully")
        const DetailsCurrentYear = await data_model.find({}, { _id: 0 }, { session:session10 });
        const DetailsBackupData = await Detail_archive_yearEnd.insertMany(DetailsCurrentYear, {session: session10 });
        console.log(DetailsCurrentYear.length, DetailsBackupData.length, "inserted Succesfully")
        const ExpCurrentYear = await umt_exception_model.find({}, { _id: 0 }, {session:session10 });
        const ExpBackupData = await UmtExceptional_archive_yearEnd.insertMany(ExpCurrentYear, {session: session10 });
        console.log(ExpCurrentYear.length, ExpBackupData.length, "inserted Succesfully")
    

more operations to do//
return true
}catch(err){
console.log(err)
}finally{
session10.endSession()
}
    

I have used mongoose to start the session, and let me know incase I did something wrong.

if anything in transaction is failing, it's not reverting back, please let me know where I have done mistake, and I am running this in batchjob.The transaction is running and roll backing correctly in case of error but it is starting another transaction after 1 minute and it is executing the same operations again resulting in a "[Symbol(errorLabels)]: Set(1) { 'TransientTransactionError' }" error

1

There are 1 best solutions below

12
dododo On

You don't call AbortTransaction. However, mongo provides convenient withTransaction API that will simplify this work for you. See here. See also mongoose related doc.