• <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

        时间:2023-10-10
        <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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 PHP 的 Expo 推送通知 下一篇:ZF2 中的 ZendDb 如何控制事务?

                  相关文章

                  <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>

                • <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'>