Skip to content

Instantly share code, notes, and snippets.

@adammck
Last active December 16, 2015 05:19
Show Gist options
  • Save adammck/5383846 to your computer and use it in GitHub Desktop.
Save adammck/5383846 to your computer and use it in GitHub Desktop.
Example of shared_connection hack causing "Mysql2::Error: This connection is still waiting for a result"
require "active_record"
require "mysql2"
ActiveRecord::Base.establish_connection(
:adapter => "mysql2",
:host => "localhost",
:username => "root",
:database => "capybara1047")
# mysql -u root
# CREATE DATABASE capybara1047;
# USE capybara1047;
# CREATE TABLE `users` (`id` integer NOT NULL PRIMARY KEY AUTO_INCREMENT, `whatever` text);
class User < ActiveRecord::Base
end
unless User.any?
5000.times do |n|
User.create(whatever: rand(1000))
end
end
source "https://rubygems.org"
gem "connection_pool"
gem "activerecord"
gem "mysql2"
adammck@leela [rbenv:1.9.3-p194]
~/projects/capybara-pr-1047$ bundle exec ruby with-connection-pool.rb
7058629314
adammck@leela [rbenv:1.9.3-p194]
~/projects/capybara-pr-1047$ bundle exec ruby without-connection-pool.rb
/Users/adammck/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `query': Mysql2::Error: This connection is still waiting for a result, try again once you have the result: SELECT `users`.* FROM `users` (ActiveRecord::StatementInvalid)
from /Users/adammck/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `block in execute'
from /Users/adammck/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
from /Users/adammck/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /Users/adammck/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
from /Users/adammck/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `execute'
from /Users/adammck/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/mysql2_adapter.rb:211:in `execute'
from /Users/adammck/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/mysql2_adapter.rb:215:in `exec_query'
from /Users/adammck/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/mysql2_adapter.rb:224:in `select'
from /Users/adammck/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/database_statements.rb:18:in `select_all'
from /Users/adammck/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
from /Users/adammck/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/querying.rb:38:in `block in find_by_sql'
from /Users/adammck/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/explain.rb:26:in `logging_query_plan'
from /Users/adammck/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/querying.rb:37:in `find_by_sql'
from /Users/adammck/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/relation.rb:171:in `exec_queries'
from /Users/adammck/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/relation.rb:160:in `block in to_a'
from /Users/adammck/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/explain.rb:26:in `logging_query_plan'
from /Users/adammck/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/relation.rb:159:in `to_a'
from /Users/adammck/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/relation/finder_methods.rb:159:in `all'
from /Users/adammck/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/querying.rb:5:in `all'
from /Users/adammck/projects/capybara-pr-1047/test.rb:4:in `block (2 levels) in run_test'
def run_test
$threads = 10.times.map do |n|
Thread.new do
# Deliberately waste time
User.all.to_a.sample.update_attributes(whatever: rand(1000))
ActiveRecord::Base.connection.close
print n
end
end
$threads.each do |thread|
thread.join
end
end
require "connection_pool"
require "./database"
require "./test"
class ActiveRecord::Base
mattr_accessor :shared_connection
@@shared_connection = nil
def self.connection
@@shared_connection || ConnectionPool::Wrapper.new(:size => 1) { retrieve_connection }
end
end
ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
run_test
require "./database"
require "./test"
class ActiveRecord::Base
mattr_accessor :shared_connection
@@shared_connection = nil
def self.connection
@@shared_connection || retrieve_connection
end
end
ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
run_test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment