allourideas/pairwise-api — Issue #15: MySQL Deadlock error in choices#update
Author: lukebaker
Created: 2011-10-11 14:24:33+00:00
State: open
Labels: bug

We have had a couple instances of a deadlock error in the choices#update method, when executing an update on the questions table to update counters.

```
ActiveRecord::StatementInvalid: Mysql::Error: Deadlock found when trying to get lock; try restarting transaction: UPDATE `questions` SET `inactive_choices_count` = 10, `updated_at` = '2011-10-05 13:23:09', `version` = 12 WHERE `id` = 1768
```
**Backtrace**

```
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract_adapter.rb:219:in `log_without_newrelic_instrumentation'
[GEM_ROOT]/gems/newrelic_rpm-3.0.0/lib/new_relic/agent/instrumentation/rails/active_record_instrumentation.rb:61:in `log'
[GEM_ROOT]/gems/newrelic_rpm-3.0.0/lib/new_relic/agent/method_tracer.rb:193:in `trace_execution_scoped'
[GEM_ROOT]/gems/newrelic_rpm-3.0.0/lib/new_relic/agent/instrumentation/rails/active_record_instrumentation.rb:58:in `log'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/connection_adapters/mysql_adapter.rb:323:in `execute'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/database_statements.rb:265:in `update_sql'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/connection_adapters/mysql_adapter.rb:338:in `update_sql'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/database_statements.rb:49:in `update_without_query_dirty'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/query_cache.rb:18:in `update'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/base.rb:2883:in `update_without_lock'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/locking/optimistic.rb:70:in `update_without_dirty'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/dirty.rb:146:in `update_without_timestamps'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/timestamp.rb:64:in `update_without_callbacks'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/callbacks.rb:282:in `update'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/base.rb:2874:in `create_or_update_without_callbacks'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/callbacks.rb:250:in `create_or_update'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/base.rb:2538:in `save_without_validation'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/validations.rb:1078:in `save_without_dirty'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/dirty.rb:79:in `save_without_transactions'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/transactions.rb:229:in `send'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/transactions.rb:229:in `with_transaction_returning_status'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/database_statements.rb:136:in `transaction'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/transactions.rb:182:in `transaction_without_trace_ActiveRecord_self_name_transaction'
[GEM_ROOT]/gems/newrelic_rpm-3.0.0/lib/new_relic/agent/method_tracer.rb:394:in `transaction'
[GEM_ROOT]/gems/newrelic_rpm-3.0.0/lib/new_relic/agent/method_tracer.rb:193:in `trace_execution_scoped'
[GEM_ROOT]/gems/newrelic_rpm-3.0.0/lib/new_relic/agent/method_tracer.rb:389:in `transaction'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/transactions.rb:228:in `with_transaction_returning_status'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/transactions.rb:196:in `save'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/transactions.rb:208:in `rollback_active_record_state!'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/transactions.rb:196:in `save'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/base.rb:2622:in `update_attribute'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/associations/association_proxy.rb:217:in `send'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/associations/association_proxy.rb:217:in `method_missing'
app/models/choice.rb:33:in `update_questions_counter'
[GEM_ROOT]/gems/activesupport-2.3.5/lib/active_support/callbacks.rb:178:in `send'
[GEM_ROOT]/gems/activesupport-2.3.5/lib/active_support/callbacks.rb:178:in `evaluate_method'
[GEM_ROOT]/gems/activesupport-2.3.5/lib/active_support/callbacks.rb:166:in `call'
[GEM_ROOT]/gems/activesupport-2.3.5/lib/active_support/callbacks.rb:93:in `run'
[GEM_ROOT]/gems/activesupport-2.3.5/lib/active_support/callbacks.rb:92:in `each'
[GEM_ROOT]/gems/activesupport-2.3.5/lib/active_support/callbacks.rb:92:in `send'
[GEM_ROOT]/gems/activesupport-2.3.5/lib/active_support/callbacks.rb:92:in `run'
[GEM_ROOT]/gems/activesupport-2.3.5/lib/active_support/callbacks.rb:276:in `run_callbacks'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/callbacks.rb:344:in `callback'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/callbacks.rb:251:in `create_or_update'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/base.rb:2538:in `save_without_validation'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/validations.rb:1078:in `save_without_dirty'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/dirty.rb:79:in `save_without_transactions'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/transactions.rb:229:in `send'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/transactions.rb:229:in `with_transaction_returning_status'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/database_statements.rb:136:in `transaction'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/transactions.rb:182:in `transaction_without_trace_ActiveRecord_self_name_transaction'
[GEM_ROOT]/gems/newrelic_rpm-3.0.0/lib/new_relic/agent/method_tracer.rb:394:in `transaction'
[GEM_ROOT]/gems/newrelic_rpm-3.0.0/lib/new_relic/agent/method_tracer.rb:193:in `trace_execution_scoped'
[GEM_ROOT]/gems/newrelic_rpm-3.0.0/lib/new_relic/agent/method_tracer.rb:389:in `transaction'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/transactions.rb:228:in `with_transaction_returning_status'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/transactions.rb:196:in `save'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/transactions.rb:208:in `rollback_active_record_state!'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/transactions.rb:196:in `save'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/base.rb:2629:in `update_attributes'
vendor/gems/josevalim-inherited_resources-0.9.1/lib/inherited_resources/actions.rb:48:in `update!'
app/controllers/choices_controller.rb:93:in `update'
[GEM_ROOT]/gems/actionpack-2.3.5/lib/action_controller/base.rb:1331:in `send'
[GEM_ROOT]/gems/actionpack-2.3.5/lib/action_controller/base.rb:1331:in `perform_action_without_filters'
[GEM_ROOT]/gems/actionpack-2.3.5/lib/action_controller/filters.rb:617:in `call_filters'
[GEM_ROOT]/gems/actionpack-2.3.5/lib/action_controller/filters.rb:610:in `perform_action_without_benchmark'
[GEM_ROOT]/gems/actionpack-2.3.5/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
[GEM_ROOT]/gems/activesupport-2.3.5/lib/active_support/core_ext/benchmark.rb:17:in `ms'
/usr/lib/ruby/1.8/benchmark.rb:308:in `realtime'
[GEM_ROOT]/gems/activesupport-2.3.5/lib/active_support/core_ext/benchmark.rb:17:in `ms'
[GEM_ROOT]/gems/actionpack-2.3.5/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
[GEM_ROOT]/gems/actionpack-2.3.5/lib/action_controller/rescue.rb:160:in `perform_action_without_flash'
[GEM_ROOT]/gems/actionpack-2.3.5/lib/action_controller/flash.rb:146:in `perform_action_without_newrelic_trace'
[GEM_ROOT]/gems/newrelic_rpm-3.0.0/lib/new_relic/agent/instrumentation/controller_instrumentation.rb:255:in `perform_action'
[GEM_ROOT]/gems/newrelic_rpm-3.0.0/lib/new_relic/agent/method_tracer.rb:193:in `trace_execution_scoped'
[GEM_ROOT]/gems/newrelic_rpm-3.0.0/lib/new_relic/agent/instrumentation/controller_instrumentation.rb:248:in `perform_action'
[GEM_ROOT]/gems/actionpack-2.3.5/lib/action_controller/base.rb:532:in `send'
[GEM_ROOT]/gems/actionpack-2.3.5/lib/action_controller/base.rb:532:in `process_without_filters'
[GEM_ROOT]/gems/actionpack-2.3.5/lib/action_controller/filters.rb:606:in `process'
[GEM_ROOT]/gems/actionpack-2.3.5/lib/action_controller/base.rb:391:in `process'
[GEM_ROOT]/gems/actionpack-2.3.5/lib/action_controller/base.rb:386:in `call'
[GEM_ROOT]/gems/actionpack-2.3.5/lib/action_controller/routing/route_set.rb:437:in `call'
[GEM_ROOT]/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:87:in `dispatch'
[GEM_ROOT]/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:121:in `_call'
[GEM_ROOT]/gems/actionpack-2.3.5/lib/action_controller/dispatcher.rb:130:in `build_middleware_stack'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/query_cache.rb:29:in `call'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/query_cache.rb:29:in `call'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/query_cache.rb:34:in `cache'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/query_cache.rb:9:in `cache'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/query_cache.rb:28:in `call'
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:361:in `call'
[GEM_ROOT]/gems/newrelic_rpm-3.0.0/lib/new_relic/rack/browser_monitoring.rb:12:in `call'
[GEM_ROOT]/gems/actionpack-2.3.5/lib/action_controller/string_coercion.rb:25:in `call'
[GEM_ROOT]/gems/rack-1.0.1/lib/rack/head.rb:9:in `call'
[GEM_ROOT]/gems/rack-1.0.1/lib/rack/methodoverride.rb:24:in `call'
[GEM_ROOT]/gems/actionpack-2.3.5/lib/action_controller/params_parser.rb:15:in `call'
[GEM_ROOT]/gems/actionpack-2.3.5/lib/action_controller/session/cookie_store.rb:93:in `call'
[GEM_ROOT]/gems/actionpack-2.3.5/lib/action_controller/failsafe.rb:26:in `call'
[GEM_ROOT]/gems/rack-1.0.1/lib/rack/lock.rb:11:in `call'
[GEM_ROOT]/gems/rack-1.0.1/lib/rack/lock.rb:11:in `synchronize'
[GEM_ROOT]/gems/rack-1.0.1/lib/rack/lock.rb:11:in `call'
```