Mongoose startSession()挂起

q3qa4bjr  于 5个月前  发布在  Go
关注(0)|答案(3)|浏览(57)

我使用mongoose通过我的nodejs服务器连接到我的Mongodb Atlas集群。
有一个特定的操作是作为一个事务来完成的。Mongoose需要调用mongoose.startSession()来启动一个事务。这个mongoose.startSession()调用很少会无限期地挂起。没有特定的方法来重现这个。

log.info('starting lock session');
const mongoSession = await mongoose.startSession();
log.info('lock session started');

字符串
在上面的代码中,starting lock session.被记录,但是当问题发生时,lock session started没有被记录。
我连接到数据库如下:

const dburl = 'mongodb+srv://myuser:[email protected]/mydb?retryWrites=true&w=majority';
mongoose.connect(dburl, {useNewUrlParser: true}, err => {
    if (err) {
        log.warn('Error occurred when connecting to database. ' + err);
    }
});


这可能是什么原因?这可能是由于数据库的一些错误?有什么方法可以进一步解决这个问题?

piwo6bdm

piwo6bdm1#

我通过使用来自mongoose.createConnection(uri, options)连接对象解决了这个问题

const connection = mongoose.createConnection(uri, options);
const session = await connection.startSession();
session.startTransaction();
await MySchema.create({
    value: "Created?",
    session: session, // giving session here
});
await session.commitTransaction();
session.endSession();

字符串
mongoose.connection与此连接对象不同

bis0qfac

bis0qfac2#

在我的例子中,当我创建一个会话时,它经常挂起,但后来我做了以下操作,我成功地创建了一个mongodb事务:
例如,从一个帐户转移一些金额到另一个帐户:
创建我的帐户架构:

const mongoose = require('mongoose');
const {appMongoConnection} = require('../drivers/mongo/mongo.init')

const accountSchema = mongoose.Schema({
    name: {
        type: String,
        required: true
    },
    amount: {
        type: Number,
        required: true
    }
});

// modelname, schema, collectionname
module.exports = appMongoConnection.model('Account', accountSchema, 'accounts');

字符串
让我们初始化mongodb连接:

const moongoose = require('mongoose')

function setupMongoose() {
    const uri = process.env.MONGO_CONNECTION_URI;
    const options = {};
    const conn = moongoose.createConnection(uri, options);

    conn.on('connected', function () {
        console.info(`${conn.name} default connection is open`);
    });
    conn.on('disconnected', function () {
        console.info(`${conn.name} default connection is disconnected`);
    });

    return conn
}

const appMongoConnection = setupMongoose();

module.exports = {
    appMongoConnection
};


现在我的路由器看起来像:

const Account = require('../models/account.model')
const express = require('express');
const mongoose = require('mongoose');
const {appMongoConnection} = require('../drivers/mongo/mongo.init')

const router = express.Router();

router.post('/transfer', async function handle(req, res, next) {
    console.log("Request hit 2");
    let mongoClient = appMongoConnection.getClient();
    
    const session = await mongoClient.startSession();
    const txnOptions = {
        readPreference: "primary",
        readConcern: { level: "local" },
        writeConcern: { w: "majority" }
    };

    try {
        // Since, we have written our code inside this block, if update fails, we need not
        // explicitly call await session.abortTransaction(); to rollback transaction
        await session.withTransaction(async () => {
            const opts = { session };
            let { from, to, amount } = req.body;

            const sender = await Account.findById(from).session(session);
            if (!sender) throw new Error('Sender account not found');

            const receiver = await Account.findById(to).session(session);
            if (!receiver) throw new Error('Receiver account not found');

            sender.amount -= amount;
            receiver.amount += amount;

            await sender.save(opts);

            // if (sender !== null) {
            //     throw new Error("Intentionally raising exception");
            // }

            await receiver.save(opts);

            // Commit transaction
            await session.commitTransaction();

            res.status(200).json({ message: 'Transaction successful' });
        }, txnOptions);
    } catch (error) {
        console.error('Transaction error:', error);
        res.status(500).json({ message: 'Transaction failed' });
    } finally {
        await session.endSession();
    }
});

module.exports = router;


mwg9r5ms

mwg9r5ms3#

这看起来像是mongoose中的一个bug,我向mongoose报告了它,但仍然没有得到回应。
https://github.com/Automattic/mongoose/issues/8325
我写了下面的函数,我可以用它来等待mongoose连接准备好,然后再调用startSession(),它解决了我的问题。

function waitForMongooseConnection(mongoose) {
    return new Promise((resolve) => {
        const connection = mongoose.connection;
        if (connection.readyState === 1) {
            resolve();
            return;
        }
        console.log('Mongoose connection is not ready. Waiting for open or reconnect event.');
        let resolved = false;
        const setResolved = () => {
            console.log('Mongoose connection became ready. promise already resolved: ' + resolved);
            if (!resolved) {
                console.log('Resolving waitForMongooseConnection');
                resolved = true;
                resolve();
            }
        };
        connection.once('open', setResolved);
        connection.once('reconnect', setResolved);
    });
}

字符串
有了上面的函数,我可以像下面这样开始会话:

log.info('starting session');
await waitForMongooseConnection(mongoose);
const mongoSession = await mongoose.startSession();
log.info('session started');


请注意,我必须关闭useUnifiedTopology。否则,'reconnect'不会被调用。

mongoose.connect(config.db, {useNewUrlParser: true, useUnifiedTopology: false}, err => {
    if (err) {
        log.warn('Error occurred when connecting to database. ' + err);
    }
});

相关问题