其实关于mysql的事务(原声mysql语句),我在我的博客里面有提到(mysql的文章分类下)

 

今天看下基于laravel框架ORM的处理

准备:

  表必须是InnoDB引擎

DB::beginTransaction();
try{
    $name = 'abc';
    $result1 = Test::create(['name'=>$name]);
    if (!$result1) {
        /**
         * Exception类接收的参数
         * $message = "", $code = 0, Exception $previous = null
         */
        throw new \Exception("1");
    }
    $result2 = Test::create(['name'=>$name]);
    if (!$result2) {
        throw new \Exception("2");
    }
    DB::commit();
} catch (\Exception $e){
    DB::rollback();//事务回滚
    echo $e->getMessage();
    echo $e->getCode();
}

注意:如果id是自增的话,mysql的primary key是在内存中维护的,事务回滚是不会回退id,所以中间会出现断层

 

 

 

转:https://www.cnblogs.com/xj76149095/p/6222066.html

相关文章:

  • 2021-10-05
  • 2021-10-31
  • 2022-12-23
  • 2021-06-28
  • 2021-11-29
猜你喜欢
  • 2022-12-23
  • 2021-07-26
  • 2021-10-24
  • 2021-09-26
相关资源
相似解决方案