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 

posted @ 2018-12-16 11:42  橘子味儿的猫  阅读(877)  评论(1编辑  收藏  举报