Laravel DB::transaction 不回滚异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24945631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:51:58  来源:igfitidea点击:

Laravel DB::transaction not rolling back on exception

phpmysqldatabaselaravel

提问by MikeWu

I'm having this small issue with Laravel 4.2 and DB::transaction. I was having an issue with transactions not being rolled back so I tried the easiest snippet of code and put it into routes.php for testing purposes:

我在 Laravel 4.2 和 DB::transaction 上遇到了这个小问题。我遇到了事务没有回滚的问题,所以我尝试了最简单的代码片段并将其放入 routes.php 以进行测试:

routes.php:

路线.php:

DB::transaction(function(){

  $user = App::make("User");
  $user->save();
  throw new Exception("Should not create users");
});
...
...
... 
Some other code here 

Simply I try to create user within transaction closure and after user is created I throw exception to force rollback of transaction. My problem is that even thou exception is thrown, the transaction does not roll back. I'm getting new user in the database every time I refresh the app. The same code works as intended on the local machine, but on the server i'm planning to use for production it simply does not roll the transaction back. Do you have any ideas why ?

只是我尝试在事务关闭中创建用户,并在创建用户后抛出异常以强制回滚事务。我的问题是,即使抛出异常,事务也不会回滚。每次刷新应用程序时,我都会在数据库中获取新用户。相同的代码在本地机器上按预期工作,但在我计划用于生产的服务器上,它根本不会回滚事务。你有什么想法吗?

EDIT:

编辑:

Server MySql: 5.1.73-cll - MySQL Community Server (GPLv2)

服务器 MySql:5.1.73-cll - MySQL 社区服务器 (GPLv2)

Server PHP: PHP 5.4.30 (cli) (built: Jul 19 2014 15:22:18)

服务器 PHP:PHP 5.4.30 (cli)(构建时间:2014 年 7 月 19 日 15:22:18)

Local PHP: 5.5.9

本地 PHP:5.5.9

Local MySql: 5.6.16

本地 MySql:5.6.16

Server is sitting on CentOs while local machine is Windows 7.

服务器在 CentOs 上,而本地机器是 Windows 7。

回答by MikeWu

So I'm responding to my own question. InnoDb was not a default storage engine until MySql 5.5. In my case MYISAM was the default storage engine and did not support the transactions. What I had to do is enable InnoDB in my CPanel server installation of MySQL. Than I had to make sure each of the tables in my Laravel migrations was created with InnoDB engine. I did that by adding:

所以我在回答我自己的问题。InnoDb 在 MySql 5.5 之前不是默认的存储引擎。就我而言,MYISAM 是默认存储引擎,不支持事务。我必须做的是在我的 CPanel 服务器安装的 MySQL 中启用 InnoDB。比我必须确保我的 Laravel 迁移中的每个表都是使用 InnoDB 引擎创建的。我通过添加:

     $table->engine = "InnoDB"; 

to each migration file. Once all the tables were set up with InnoDB engine, transactions work as intended.

到每个迁移文件。一旦使用 InnoDB 引擎设置了所有表,事务就会按预期工作。

回答by Saad Bhutto

An alternative could be the settings in

另一种可能是设置

/config/database.php

/config/database.php

change value of engine from null to InnoDB

将引擎的值从 null 更改为 InnoDB

'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => 'InnoDB',
        ],

回答by Daniel Renteria

If you are using multiple databases on your project, you need to specify in which database you want to commit and rollback.

如果在项目中使用多个数据库,则需要指定要在哪个数据库中提交和回滚。

DB::connection('name_of_database')->beginTransaction();
    try {
        //Do something
        DB::connection('name_of_database')->commit();

    } catch (Exception $e) {
        DB::connection('name_of_database')->rollback();
    }