• <tfoot id='LK9kK'></tfoot>

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

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

        如何使用 Doctrine 在死锁后重试事务?

        How to retry transaction after a deadlock using Doctrine?(如何使用 Doctrine 在死锁后重试事务?)
          <tbody id='ey757'></tbody>

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

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

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

                • 本文介绍了如何使用 Doctrine 在死锁后重试事务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在编写一个 PHP 函数,它将大量数据存储/更新到一个表中,这可能会导致死锁.我尝试调查如何使用 Doctrine 重试失败的交易,但遗憾的是在网上找不到任何信息.我最终写了下面的代码

                  I am writing a PHP function which store/updates large sets of data into a table and that may cause a deadlock. I tried investigating how to retry a failed transaction with Doctrine but sadly could not find any info online. I eventually wrote the following code

                   $retry = 0;
                   $done = false;
                   while (!$done and $retry < 3) {
                       try {
                  
                           $this->entityManager->flush();
                           $done = true;
                  
                       } catch (Exception $e) {
                           sleep(1);
                  
                           $retry++;
                       }
                   }
                  
                   if ($retry == 3) {
                       throw new Exception(
                           "[Exception: MySQL Deadlock] Too many people accessing the server at the same time. Try again in few minutes"
                       );
                   }
                  

                  我的问题:这种方法是否有可能在数据库中插入重复项?如果是这样,我如何强制 Doctrine 回滚事务?

                  My question: is there a chance this approach will insert duplicates in the database? if so, how can I force Doctrine to roll back the transactions?

                  推荐答案

                  死锁返回错误 1213,您应该在客户端处理

                  A deadlock returns error 1213 which you should process on the client side

                  请注意,死锁和锁等待是不同的事情.在僵局中,没有失败"的交易:他们都是有罪的.无法保证回滚哪一个.

                  Note that a deadlock and lock wait are different things. In a deadlock, there is no "failed" transaction: they are both guilty. There is no guarantee which one will be rolled back.

                  您必须使用rollback,您的样式代码将插入重复项.例如你应该:

                  You must use rollback, your style code will insert duplicate. for example you should :

                  $retry = 0;
                  
                  $done = false;
                  
                  
                  $this->entityManager->getConnection()->beginTransaction(); // suspend auto-commit
                  
                  while (!$done and $retry < 3) {
                  
                      try {
                  
                          $this->entityManager->flush();
                  
                          $this->entityManager->getConnection()->commit(); // commit if succesfull
                  
                          $done = true;
                  
                      } catch (Exception $e) {
                  
                          $this->entityManager->getConnection()->rollback(); // transaction marked for rollback only
                  
                          $retry++;
                  
                      }
                  
                  }
                  

                  希望对您有所帮助.

                  这篇关于如何使用 Doctrine 在死锁后重试事务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 的问题)
                  • <legend id='aLexK'><style id='aLexK'><dir id='aLexK'><q id='aLexK'></q></dir></style></legend>
                      <tbody id='aLexK'></tbody>

                      <bdo id='aLexK'></bdo><ul id='aLexK'></ul>

                        1. <small id='aLexK'></small><noframes id='aLexK'>

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