Laravel数据库事务无法工作

8
我是一名有用的助手,可以为您翻译文本。
我正在尝试在Laravel 5.5中设置数据库事务,但似乎无法正常工作。我使用MySQL 5.7.20,并且所有来自tools模式的表都是InnoDB。我还运行PHP 7.2.3。
我有以下代码:
DB::beginTransaction();
try {
    // checks whether the users marked for removal are already assigned to the list
    foreach ($removeStudents as $removeStudent) {
        if ($ls->allocatedStudents->find($removeStudent->id) === null) {
            throw new Exception('userNotAllocated', $removeStudent->id);
        } else {
            DB::connection('tools')->table('exercises_list_allocations')
                ->where('user_id', $removeStudent->id)
                ->where('exercises_list_id', $ls->id)
                ->delete();
        }
    }

    // checks whether the users marked for removal are already assigned to the list
    foreach ($addStudents as $addStudent) {
        if ($ls->allocatedStudents->find($addStudent->id) === null) {
            DB::connection('tools')->table('exercises_list_allocations')
               ->insert([
                   'user_id' => $addStudent->id,
                   'exercises_list_id' => $ls->id
               ]);
        } else {
            throw new Exception('userAlreadyAllocated', $addStudent->id);
        }
    }

    DB::commit();
} catch (Exception $e) {
    DB::rollBack();
    return response()->json(
        [
            'error' => $e->getMessage(),
            'user_id' => $e->getCode()
        ], 400
    );
}

它不会回滚事务。如果在一些删除或插入操作之后发现异常,则不会恢复它们。

起初我以为这可能是MySQL的问题,所以我尝试手动运行以下SQL查询:

START TRANSACTION;
DELETE FROM tools.exercises_list_allocations WHERE user_id = 67 AND exercises_list_id=308;
DELETE FROM tools.exercises_list_allocations WHERE user_id = 11479 AND exercises_list_id=308;
INSERT INTO tools.exercises_list_allocations (user_id, exercises_list_id) VALUES (1,308);
INSERT INTO tools.exercises_list_allocations (user_id, exercises_list_id) VALUES (2,308);
INSERT INTO tools.exercises_list_allocations (user_id, exercises_list_id) VALUES (3,308);
ROLLBACK;

而且它回滚了所有的删除和插入操作(正如预期的那样),tools.exercises_list_allocations表中没有发生任何更改,因此我排除了数据库服务器是问题的原因。
因此,我认为应该是PHP代码有问题。我在网上搜索了与我的问题类似的问题,并尝试了一些报告的解决方案。
我尝试使用DB :: transaction()方法来代替try/catch块中的匿名函数,但没有成功。
我尝试使用Eloquent ORM而不是DB::insert()和DB::delete()方法,同时尝试了DB::transaction()方法与匿名函数以及DB::beginTransaction(),DB::commit()和DB::rollback()方法,但都没有成功。
我尝试禁用严格模式并在config/database.php中强制将引擎设置为InnoDB,但也没有成功。
我做错了什么?我需要所有的删除和插入操作在一个原子事务中运行。

我真的没有看到任何问题。你没有收到任何错误吗?可能是死锁了吗?有些问题在PHP中不会显示出来?我不知道,也许是什么其他的问题。 - Andrei
你能给我看一下你的database.php文件吗? - DsRaj
@Andrew 当我运行代码时,nginx、php-fpm或mysql日志中没有任何显示。除了我抛出的异常之外,没有报告任何错误。 - Matheus Boy
@DsRaj 这是工具数据库配置:'tools' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE_TOOLS', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'unix_socket' => env('DB_SOCKET', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => true, 'engine' => null, ], - Matheus Boy
你是否考虑过使用 DB::transaction() 将你的代码包装在一个事务闭包中?Laravel 将会为你处理 commitrollback,无论你在何处退出闭包。这种方式可以将必须作为事务运行的代码隔离起来 - 当你离开这个隔离区域时,事务将被保证要么提交要么回滚。 - Jason
1个回答

17

如果您和我一样,在您的应用程序中配置了多个数据库连接,则在调用事务方法之前必须选择一个连接,如ThejakaMaldeniya的评论所建议的那样,如果您想运行的查询不在默认连接中:

DB::connection('tools')->beginTransaction();
DB::connection('tools')->commit();
DB::connection('tools')->rollBack();

它运作得非常完美。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接