duanbeng6709 2012-02-13 03:24
浏览 163
已采纳

如何优雅地管理Kohana / PHP中的数据库事务

I'm new to Kohana but come from a Spring/Java background.

In Spring I'm used to all service methods automatically having DB transactions applied to them. I just tag the method to indicate whether it only needs a read transaction or read/write.

What do people do in this regard in Kohana? A new app I'm working with doesn't have transactions except manually in a few places where they know it's necessary. To me this seems a bit risky, it's easy to overlook some transactional consistency requirement, it was always nice to have that globally enforced in Spring.

  • 写回答

4条回答 默认 最新

  • dpdjpt5137 2012-02-29 07:10
    关注

    Here is how I deal with it:

    1. Create a base class for all service method classes:

      abstract class Base
      {
      
          public function __call($method,$arguments)
          {
                  // Start transaction here
                  echo 'start';
      
                  try
                  {                    
                      call_user_func_array(array($this, $method), $arguments);
      
                      // Commit
                      echo ' commit';
                  }
                  catch ($e)
                  {
                      // Roll back
                  }                       
          }
      
      }
      
    2. Create a child class with all "protected" functions:

      class Member extends Base
      {
              protected function test()
              {
                  echo 'test';
              }
      }
      
    3. Call service method:

      $member = new Member();
      
      $member->test();
      

    It will display: "start test commit"

    The trick here is you will have to use "protected" for all your functions, if not it will no longer run (it will call the "test" function directly, not through __call().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能