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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog