• <tfoot id='1YhCk'></tfoot>
      <bdo id='1YhCk'></bdo><ul id='1YhCk'></ul>

    <legend id='1YhCk'><style id='1YhCk'><dir id='1YhCk'><q id='1YhCk'></q></dir></style></legend>
  • <i id='1YhCk'><tr id='1YhCk'><dt id='1YhCk'><q id='1YhCk'><span id='1YhCk'><b id='1YhCk'><form id='1YhCk'><ins id='1YhCk'></ins><ul id='1YhCk'></ul><sub id='1YhCk'></sub></form><legend id='1YhCk'></legend><bdo id='1YhCk'><pre id='1YhCk'><center id='1YhCk'></center></pre></bdo></b><th id='1YhCk'></th></span></q></dt></tr></i><div id='1YhCk'><tfoot id='1YhCk'></tfoot><dl id='1YhCk'><fieldset id='1YhCk'></fieldset></dl></div>
  • <small id='1YhCk'></small><noframes id='1YhCk'>

        Laravel:在 DB::transaction() 中使用 try...catch

        Laravel: Using try...catch with DB::transaction()(Laravel:在 DB::transaction() 中使用 try...catch)
        <legend id='SFYlZ'><style id='SFYlZ'><dir id='SFYlZ'><q id='SFYlZ'></q></dir></style></legend>
            <tbody id='SFYlZ'></tbody>

        1. <i id='SFYlZ'><tr id='SFYlZ'><dt id='SFYlZ'><q id='SFYlZ'><span id='SFYlZ'><b id='SFYlZ'><form id='SFYlZ'><ins id='SFYlZ'></ins><ul id='SFYlZ'></ul><sub id='SFYlZ'></sub></form><legend id='SFYlZ'></legend><bdo id='SFYlZ'><pre id='SFYlZ'><center id='SFYlZ'></center></pre></bdo></b><th id='SFYlZ'></th></span></q></dt></tr></i><div id='SFYlZ'><tfoot id='SFYlZ'></tfoot><dl id='SFYlZ'><fieldset id='SFYlZ'></fieldset></dl></div>

            <tfoot id='SFYlZ'></tfoot>

              • <bdo id='SFYlZ'></bdo><ul id='SFYlZ'></ul>

                  <small id='SFYlZ'></small><noframes id='SFYlZ'>

                  本文介绍了Laravel:在 DB::transaction() 中使用 try...catch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我们都使用 DB::transaction() 进行多个插入查询.这样做时,应该将 try...catch 放在里面还是包装它?如果出现问题,事务将自动失败,是否还需要包含 try...catch ?

                  We all use DB::transaction() for multiple insert queries. In doing so, should a try...catch be placed inside it or wrapping it? Is it even necessary to include a try...catch when a transaction will automatically fail if something goes wrong?

                  示例 try...catch 包装交易:

                  // try...catch
                  try {
                      // Transaction
                      $exception = DB::transaction(function() {
                  
                          // Do your SQL here
                  
                      });
                  
                      if(is_null($exception)) {
                          return true;
                      } else {
                          throw new Exception;
                      }
                  
                  }
                  catch(Exception $e) {
                      return false;
                  }
                  

                  相反,一个 DB::transaction() 包装了一个 try...catch:

                  The opposite, a DB::transaction() wrapping a try...catch:

                  // Transaction
                  $exception = DB::transaction(function() {
                      // try...catch
                      try {
                  
                          // Do your SQL here
                  
                      }
                      catch(Exception $e) {
                          return $e;
                      }
                  
                  });
                  
                  return is_null($exception) ? true : false;
                  

                  或者只是一个没有 try...catch 的事务

                  Or simply a transaction w/o a try...catch

                  // Transaction only
                  $exception = DB::transaction(function() {
                  
                      // Do your SQL here
                  
                  });
                  
                  return is_null($exception) ? true : false;
                  

                  推荐答案

                  如果您需要通过代码手动退出"事务(无论是通过异常还是只是检查错误状态),您不应该使用 DB::transaction() 而是将您的代码包装在 DB::beginTransactionDB::commit/DB::rollback():

                  In the case you need to manually 'exit' a transaction through code (be it through an exception or simply checking an error state) you shouldn't use DB::transaction() but instead wrap your code in DB::beginTransaction and DB::commit/DB::rollback():

                  DB::beginTransaction();
                  
                  try {
                      DB::insert(...);
                      DB::insert(...);
                      DB::insert(...);
                  
                      DB::commit();
                      // all good
                  } catch (Exception $e) {
                      DB::rollback();
                      // something went wrong
                  }
                  

                  请参阅交易文档.

                  这篇关于Laravel:在 DB::transaction() 中使用 try...catch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)

                  <i id='08dck'><tr id='08dck'><dt id='08dck'><q id='08dck'><span id='08dck'><b id='08dck'><form id='08dck'><ins id='08dck'></ins><ul id='08dck'></ul><sub id='08dck'></sub></form><legend id='08dck'></legend><bdo id='08dck'><pre id='08dck'><center id='08dck'></center></pre></bdo></b><th id='08dck'></th></span></q></dt></tr></i><div id='08dck'><tfoot id='08dck'></tfoot><dl id='08dck'><fieldset id='08dck'></fieldset></dl></div>
                        <tbody id='08dck'></tbody>

                    • <legend id='08dck'><style id='08dck'><dir id='08dck'><q id='08dck'></q></dir></style></legend><tfoot id='08dck'></tfoot>
                        <bdo id='08dck'></bdo><ul id='08dck'></ul>

                        <small id='08dck'></small><noframes id='08dck'>