Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Lock Wait Timeout using Model Factories

Tags:

People also ask

What Causes lock wait timeout mysql?

The common causes are: The offensive transaction is not fast enough to commit or rollback the transaction within innodb_lock_wait_timeout duration. The offensive transaction is waiting for row lock to be released by another transaction.


I have a very small dataset being generated in a method on a Test Class, I had tried having the data generated in the setUp() method but it was leading to a lock wait timeout for every test. Moving this code out of the setUp() method and putting it in it's own method has helped a bit. Meaning that not every test complains about the locks.

I am using the DatabaseTransactions trait that is in Laravel 5.2 so that the database is reset before each test case is run.

In every test case that I have I make a call to get the mock data like this.

$data = self::getRandomCommunityWithAssociatedData(); 

The actual method only generates a few community objects and user objects for the created community.

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]]; } 

There are a few constants in use in that method and they are used to determine how many of each object to create. Currently I am creating 2 communities and 3 users and 2 admins for each community instance.

The lock wait timeouts are not predictable, one run it could happen on the first test case, another run it could happen on the 5th test case.

I have tried to increase the time in which MySQL waits for the lock to 500 seconds and I am still getting the timeouts. Increasing this time really is not an option because the tests need to be able to run on all environments.

Any thoughts as to why I might be getting these lock wait timeouts when using the DatabaseTransactions trait in Laravel 5.2 with such a small dataset?

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

The above query that is executed is a simple insert on the community table and there are no complicated subqueries or anything like that going on in this data generation.

Enabled the query log, results.

       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