<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 事務 [TOC=2,3] 模型中提供了對事務操作的支持,但前提需要數據庫支持事務。 `Mysql`?中的?`InnoDB`?和?`BDB`?存儲引擎支持事務,如果在 Mysql 下使用事務的話,需要將數據庫的存儲引擎設置為 InnoDB 或 BDB。 `SQLite`?直接支持事務。 ## 使用事務 模型中提供了?`startTrans`,`commit`?和?`rollback`?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 方法 使用事務時,要一直使用?`startTrans`,`commit`?和?`rollback`?這 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 接收一個回調函數,這個回調函數中處理真正的邏輯,并需要返回。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看