nodejs sql --- 添加事务

添加事务

        let transaction; 

        try {
            transaction = await this.ctx.model.transaction();

            await this.ctx.model.Collect.create({id: 39, author: 'zyu111', count: 1}, {transaction})

            await this.ctx.model.Collect.create({id: 42, author: 'zyu8228', count: 1}, {transaction})

            await transaction.commit();

        } catch (err) {
            // console.log(err)
            console.log('回滚了。。。')
            await transaction.rollback();
        }

事务的操作对象 必须是 model 

猜你喜欢

转载自www.cnblogs.com/zyulike/p/10126033.html