分享

4.事务

 念念爸 2016-06-28

事务

模型中提供了对事务操作的支持,但前提需要数据库支持事务。

Mysql 中的 InnoDBBDB 存储引擎支持事务,如果在 Mysql 下使用事务的话,需要将数据库的存储引擎设置为 InnoDB 或 BDB。

SQLite 直接支持事务。

使用事务

模型中提供了 startTranscommitrollback 3 种方法来操作事务。

  • startTrans 开启事务
  • commit 正常操作后,提交事务
  • rollback 操作异常后进行回滚

ES6 方式

export default class extends think.controller.base {
  * indexAction(){
    let model = this.model("user");
    try{
      yield model.startTrans();
      let userId = yield model.add({name: "xxx"});
      let insertId = yield this.model("user_group").add({user_id: userId, group_id: 1000});
      yield model.commit();
    }catch(e){
      yield model.rollback();
    }
  }
}

动态创建类的方式

module.exports = think.controller({
  indexAction: function(self){
    var model = this.model("user");
    return model.startTrans().then(function(){
      return model.add({name: "xxx"});
    }).then(function(userId){
      return self.model("user_group").add({user_id: userId, group_id: 1000})
    }).then(function(){
      return self.commit();
    }).catch(function(err){
      return self.rollback();
    });
  }
})

transaction 方法

使用事务时,要一直使用 startTranscommitrollback 这 3 个方法进行操作,使用起来有一些不便。为了简化这一操作,模型中提供了 transaction 方法来更加方便的处理事务。

ES6 方式

export default class extends think.controller.base {
  * indexAction(self){
    let model = this.model("user");
    let insertId = yield model.transaction( function * (){
      let userId = yield model.add({name: "xxx"});
      return yield self.model("user_group").add({user_id: userId, group_id: 1000});
    })
  }
}

注:Arrow Function 无法和 */yield 一起写,所以上面为 function *。如果想使用 Arrow Function,可以使用 async,如: async () => {}

使用动态创建类的方式

module.exports = think.controller({
  indexAction: function(self){
    var model = this.model("user");
    return model.transaction(function(){
      return model.add({name: "xxx"}).then(function(userId){
        return self.model("user_group").add({user_id: userId, group_id: 1000});
      });
    }).then(function(insertId){

    }).catch(function(err){

    })
  }
})

transaction 接收一个回调函数,这个回调函数中处理真正的逻辑,并需要返回。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多