ThinkPHP事务支持及MySQL事务浅析

一 ,ThinkPHP5.1同一类文件中,方法A调用方法B,A、B方法中都有事务,事务独立运行


public function a(){

    Db::startTrans();
    try{

        Order::where('id',1)->update(['platform_id'=>'32']);
        $this->b();
        Db::rollback();
        Order::where('id',1)->update(['order_no'=>'77777']);
        Db::rollback();

    }catch (Exception $e){

        trace($e->getMessage(),'b');
        Db::rollback();

    }

}


public function b(){

    Db::startTrans();
    try{

        Wallet::where('id',1)->update(['wallet_id_'=>'123123']);
        Db::commit();

    }catch (Exception $e){

        trace($e->getMessage(),'b');
        Db::rollback();

    }

}


*两个事务用到的并不是同一个数据库连接



二 ,MySQL事务嵌套,里层触发外出事务无条件提交,同时外层事务在里层处理外之后的每条SQL以单独事务形式执行



CREATE DEFINER=`bp_user`@`` PROCEDURE `new_procedure`()
BEGIN
start transaction;
    update `order` set order_no = '123456' where id = 1;

    start transaction;

        update `order` set platform_id = '666' where id = 1;

    commit;

    update `order` set order_no = concat_ws('_',order_no,'4544534') where id = 1;

commit;
END



上述存储过程运行结果:



评论0条