简易嵌套事务实现

简易嵌套事务实现

class Tran {
    private static $num = 0;
    private static $flag = true;

    //开启事务
    public static function tranStart(){
        self::$num++;
        if(self::$num == 1){
            \DB::beginTransaction();
        }
    }

    //回滚事务
    public static function tranClose(){
        self::$num = 0;
        self::$flag = false;
        \DB::rollBack();
    }

    //提交事务
    public static function tranCommit(){
        self::$num--;
        if(self::$num == 0 && true === self::$flag){
            \DB::commit();
            return true;
        }else{
            return false;
        }
    }

    //强制重新开启事务
    public static function tranForceStart(){
        self::$num = 1;
        self::$flag = true;
        if(self::$num == 1){
            \DB::beginTransaction();
        }
    }

    public static function getNumber(){
        return self::$num;
    }
}
相关文章
相关标签/搜索