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

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

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

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

    3. <legend id='VRS3J'><style id='VRS3J'><dir id='VRS3J'><q id='VRS3J'></q></dir></style></legend>
      1. 多个服务层和数据库事务

        Multiple Service Layers and Database Transactions(多个服务层和数据库事务)

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

            <legend id='oTyku'><style id='oTyku'><dir id='oTyku'><q id='oTyku'></q></dir></style></legend>

              <tfoot id='oTyku'></tfoot>

                • 本文介绍了多个服务层和数据库事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我只是想知道如何最好地处理跨多个服务层的事务.服务层使用 ORM 来存储和检索数据库.是否应该在各个服务层中了解和处理事务?还是应该由另一层处理?

                  I'm just wondering how to best handle transactions across multiple service layers. The service layers use an ORM to store and retrieve from the database. Should the transactions be known and handled within the individual service layers? Or should they be handled by another layer?

                  例如:我为用户和客户提供两个服务层.我想:

                  For example: I have two service layers for users and clients. I would like to:

                  1) 创建并保存一个新客户端
                  2) 创建并保存一个新用户
                  3) 将该用户分配给客户端

                  1) Create and save a new client
                  2) Create and save a new user
                  3) Assign that user to the client

                  所有在一个事务中.

                  一个简单的例子可能如下所示:

                  A simple example might look like this:

                  $userManagementService = new UserManagementService;
                  $newUserData = array(...);
                  $newUser = $userManagementService->create($newUserData);
                  
                  $clientManagementService = new ClientManagementService;
                  $newClientData = array(...);
                  $newClient = $clientManagementService->create($newClientData);
                  
                  $userManagementService->assignUserToClient($newUser, $newClient);
                  

                  事务逻辑应该去哪里?

                  推荐答案

                  不要尝试在服务层或 ORM 内进行嵌套事务.

                  Do not try to do nested transactions within service layers or within the ORM.

                  事务对于数据库连接是全局的.除非您的 RDBMS 本身支持嵌套事务并且您的 DB API 公开嵌套事务,否则您可能会遇到异常情况.

                  Transactions are global to the DB connection. Unless your RDBMS supports nested transactions natively and your DB API exposes nested transactions, you can run into anomalies.

                  详情见我对的回答如何检测该事务已经开始?

                  由于您使用的是 PHP,因此您的事务范围至多是单个请求.所以你应该只使用容器管理的事务,而不是服务层事务.也就是说,在处理请求开始时启动事务,并在处理完请求时提交(或回滚).

                  Since you're using PHP, the scope of your transactions is at most a single request. So you should just use container-managed transactions, not service-layer transa. That is, start the transaction at the start of handling the request, and commit (or rollback) as you finish handling the request.

                  如果在嵌套的 ORM 操作中发生需要回滚的异常,则使用异常将其冒泡,并让容器(即您的 PHP 操作控制器)处理它.

                  If an exception requiring a rollback occurs deep within nested ORM actions, then bubble that up by using an Exception, and let the container (i.e. your PHP action controller) take care of it.

                  这篇关于多个服务层和数据库事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 的问题)
                    <tfoot id='dEpyf'></tfoot>

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

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

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