php - 使用模型工厂的 MySQL 锁定等待超时

标签 php mysql laravel eloquent laravel-5.2

我在测试类的方法中生成了一个非常小的数据集,我曾尝试在 setUp() 方法中生成数据,但它导致每个锁等待超时测试。将此代码从 setUp() 方法中移出并将其放入它自己的方法中有所帮助。这意味着并非每个测试都会提示锁。

我正在使用 Laravel 5.2 中的 DatabaseTransactions 特性,以便在运行每个测试用例之前重置数据库。

在我拥有的每个测试用例中,我都会像这样调用以获取模拟数据。

$data = self::getRandomCommunityWithAssociatedData();

实际的方法只是为创建的社区生成一些社区对象和用户对象。

public static function getRandomCommunityWithAssociatedData()
{
    self::$communities = factory(\Community::class, self::COMMUNITIES_TO_CREATE)->create()->each(function ($community) {
        self::$users[$community->id] = factory(User::class, self::USERS_TO_CREATE)->create()->each(function (\User $user) use ($community) {
            $user->community()->associate($community);
        });

        self::$admins[$community->id] = factory(User::class, 'superadmin', self::ADMINS_TO_CREATE)->create()->each(function (\User $admin) use ($community) {
            $admin->community()->associate($community);
        });
    });

    $community = self::$communities[mt_rand(0, count(self::$communities) - 1)];

    return ['community' => $community, 'users' => self::$users[$community->id], 'admins' => self::$admins[$community->id]];
}

该方法中使用了一些常量,它们用于确定要创建的每个对象的数量。目前,我正在为每个社区实例创建 2 个社区以及 3 个用户和 2 个管理员。

锁等待超时是不可预测的,一次运行它可能发生在第一个测试用例上,另一次运行它可能发生在第 5 个测试用例上。

我已经尝试将 MySQL 等待锁定的时间增加到 500 秒,但我仍然遇到超时。增加这个时间确实不是一种选择,因为测试需要能够在所有环境中运行。

关于为什么在 Laravel 5.2 中使用 DatabaseTransactions 特性时我可能会遇到这些锁定等待超时有什么想法吗?

1) UserEmailNotificationsTest::testActiveAdminReceivesNewCommentEmailNotification
Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction (SQL: insert into `communities` (`

上面执行的查询是对社区表的简单插入,在此数据生成中没有复杂的子查询或类似的事情。

启用查询日志、结果。

       30 Query START TRANSACTION
       30 Query SAVEPOINT trans2
       30 Prepare   insert into `communities` (`viddler_id`, `domain`, `subdomain`, `active`, `max_seats`, `created_at`, `updated_at`, `assignment_seats`, `general_settings`, `access_settings`, `branding_settings`) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
       30 Execute   insert into `communities` (`viddler_id`, `domain`, `subdomain`, `active`, `max_seats`, `created_at`, `updated_at`, `assignment_seats`, `general_settings`, `access_settings`, `branding_settings`) values ('74', 'gaylord.com', 'qui', '1', '24', '2016-03-30 16:25:45', '2016-04-04 02:27:04', '23', 'a:11:{s:5:\"title\";s:0:\"\";s:11:\"description\";s:0:\"\";s:10:\"meta_title\";s:0:\"\";s:16:\"meta_description\";s:0:\"\";s:13:\"meta_keywords\";s:0:\"\";s:17:\"thumbnail_display\";s:0:\"\";s:18:\"group_nomenclature\";s:8:\"Channels\";s:13:\"home_template\";s:0:\"\";s:11:\"api_version\";s:1:\"2\";s:8:\"language\";s:2:\"en\";s:21:\"use_nested_navigation\";i:0;}', 'a:10:{s:12:\"restrictions\";s:10:\"Restricted\";s:17:\"auto_registration\";i:0;s:14:\"default_active\";i:0;s:12:\"oauth_google\";i:0;s:14:\"oauth_facebook\";i:0;s:14:\"oauth_linkedin\";i:0;s:16:\"oauth_reg_google\";i:0;s:18:\"oauth_reg_facebook\";i:0;s:18:\"oauth_reg_linkedin\";i:0;s:11:\"lti_enabled\";i:0;}', 'a:14:{s:13:\"contact_email\";s:0:\"\";s:17:\"contact_link_text\";s:0:\"\";s:9:\"logo_file\";s:0:\"\";s:14:\"carousel_items\";s:0:\"\";s:11:\"html_header\";s:0:\"\";s:16:\"footer_copyright\";s:45:\"© 2015 Viddler Inc. All Rights Reserved.\";s:19:\"footer_privacy_link\";s:37:\"http://www.viddler.com/privacy-policy\";s:17:\"footer_terms_link\";s:35:\"http://www.viddler.com/terms-of-use\";s:14:\"help_link_text\";s:4:\"Help\";s:9:\"help_link\";s:0:\"\";s:7:\"color_1\";s:7:\"#ffffff\";s:7:\"color_2\";s:7:\"#2C333C\";s:7:\"color_3\";s:7:\"#2C333C\";s:7:\"color_4\";s:7:\"#60a1d7\";}')
160404  9:30:25    30 Close stmt
       21 Query ROLLBACK
       21 Quit
       22 Query ROLLBACK
       22 Quit
       23 Query ROLLBACK
       23 Quit
       24 Query ROLLBACK
       24 Quit
       25 Query ROLLBACK
       25 Quit
       26 Query ROLLBACK
       26 Quit
       27 Query ROLLBACK
       27 Quit
       28 Query ROLLBACK
       28 Quit
       29 Query ROLLBACK
       29 Quit
       30 Query ROLLBACK
       30 Quit

最佳答案

因此,增加锁等待超时不是最佳实践; the best practice is to catch the error and recover .

正式来说,这是你应该做的:

Both deadlocks and lock wait timeouts are normal on busy servers and it is necessary for applications to be aware that they may happen and handle them by retrying. You can make them less likely by doing as little work as possible between the first change to data during a transaction and the commit, so the locks are held for the shortest possible time and for the smallest possible number of rows. Sometimes splitting work between different transactions may be practical and helpful.

关于php - 使用模型工厂的 MySQL 锁定等待超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36403457/

相关文章:

mysql - 从单个 SQL 查询返回聚合结果行

mysql - 我可以使用子查询代替什么来搜索 MAX()?

laravel - 具有替代键的 Eloquent ManyToMany

php - 我在 Laravel 中遇到的问题是 "This site can’ 无法到达 127.0.0.1 拒绝连接。”

php - 使用 Curl 代理发布数据不返回任何内容

java - 获取网站的端口号

php - 无法根据 SQL 输出创建单独的论坛类别

php - 如何在 Laravel 中记录用户操作?如何监听 Controller 方法?

javascript - 我有在 firefox 控制台中检查过的 ajax 响应。但是我需要在 php 中将该响应显示为 html 表

php - 由于 JOIN 相同的 ID,按组删除重复结果