数据库
首页 > 数据库> > ActiveRecord :: StatementInvalid:Mysql2 :: Error:超出锁定等待超时

ActiveRecord :: StatementInvalid:Mysql2 :: Error:超出锁定等待超时

作者:互联网

在我的rails项目中,我使用sidekiq处理耗时的任务,但在sidekiq日志中出现错误:

ActiveRecord::StatementInvalid: Mysql2::Error: Lock wait timeout exceeded; try restarting transaction: UPDATE `marker_layers` SET `show_fields` = 'title,desc', `sort_col` = 'title,desc', `updated_at` = '2016-05-17 07:36:02' WHERE `marker_layers`.`id` = 16021210
Processor: iZ23edse84Z:29310

enter image description here
sidekiq.yml

# Options here can still be overridden by cmd line args.
#   setsid sidekiq -d -C config/sidekiq.yml -e production
---
:concurrency: 5
:pidfile: tmp/pids/sidekiq.pid
:logfile: log/sidekiq.log
staging:
  :concurrency: 10
production:
  :concurrency: 40
:queues:
  - ['critical', 3]
  - ['default', 2]
  - ['low', 1]

database.yml的

production:
   adapter: mysql2
   encoding: utf8mb4
   collation: utf8mb4_bin
   reconnect: false
   database: database_name
   pool: 48
   username: password
   password: password
   host: locahost

解决方法:

发生此错误的原因是,当不同的工作程序尝试修改同一资源时,事务超时,基本上是数据库死锁.

如果您正在使用显式事务,例如SomeModel.transaction {SomeModel.task_that_takes_too_much_time},或者使用修改记录的常规ActiveRecord方法,则会发生这种情况,因为所有内容都包含在事务中.

我能给你的唯一建议就是探索让你的工人与众不同的替代方案,例如使用https://github.com/mhenrixon/sidekiq-unique-jobs并让你的工作使用.perform_in.

标签:mysql,ruby-on-rails,activerecord,sidekiq
来源: https://codeday.me/bug/20190711/1430907.html