This project is archived and is in readonly mode.

#1023 ✓invalid
Jeff Rafter

Connection Pool always resets the connection

Reported by Jeff Rafter | September 11th, 2008 @ 03:09 PM | in 2.x

I have noticed that the connection pool is resetting the connection to my Mysql database for every spec in my list of specs. Apart from the fact that this creates unnecessary overhead, it means that setting up connection specific settings for the database need to be repeated (e.g. FOREIGN_KEY_CHECKS = 0 for legacy databases). Placing a breakpoint (or log) in configure_connection in the adapter shows that it is getting called every time the connection is accessed from the pool.

This is the stack trace if I place an exception in the configure method:

vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb:549:in configure_connection'
vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb:288:inreset!'
vendor/rails/activesupport/lib/active_support/callbacks.rb:178:in send'
vendor/rails/activesupport/lib/active_support/callbacks.rb:178:inevaluate_method'
vendor/rails/activesupport/lib/active_support/callbacks.rb:166:in call'
vendor/rails/activesupport/lib/active_support/callbacks.rb:90:inrun'
vendor/rails/activesupport/lib/active_support/callbacks.rb:90:in each'
vendor/rails/activesupport/lib/active_support/callbacks.rb:90:insend'
vendor/rails/activesupport/lib/active_support/callbacks.rb:90:in run'
vendor/rails/activesupport/lib/active_support/callbacks.rb:277:inrun_callbacks'
vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:201:in checkout_and_verify'
vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:196:incheckout_existing_connection'
vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:136:in checkout'
/opt/local/lib/ruby/1.8/monitor.rb:242:insynchronize'
vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:134:in checkout'
vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:64:inconnection'
vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:250:in retrieve_connection'
vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb:121:inretrieve_connection'
vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb:113:in connection'
vendor/rails/activerecord/lib/active_record/fixtures.rb:933:insetup_fixtures'
vendor/plugins/rspec-rails/lib/spec/rails/example/rails_example_group.rb:15vendor/plugins/rspec/lib/spec/example/example_methods.rb:49:in instance_eval'
vendor/plugins/rspec/lib/spec/example/example_methods.rb:49:ineval_each_fail_fast'
vendor/plugins/rspec/lib/spec/example/example_methods.rb:48:in each'
vendor/plugins/rspec/lib/spec/example/example_methods.rb:48:ineval_each_fail_fast'
vendor/plugins/rspec/lib/spec/example/example_group_methods.rb:266:in run_before_each'
vendor/plugins/rspec/lib/spec/example/example_group_methods.rb:360:inexecute_in_class_hierarchy'
vendor/plugins/rspec/lib/spec/example/example_group_methods.rb:359:in each'
vendor/plugins/rspec/lib/spec/example/example_group_methods.rb:359:inexecute_in_class_hierarchy'
vendor/plugins/rspec/lib/spec/example/example_group_methods.rb:265:in run_before_each'
vendor/plugins/rspec/lib/spec/example/example_methods.rb:101:inbefore_example'
vendor/plugins/rspec/lib/spec/example/example_methods.rb:20:in execute'
/opt/local/lib/ruby/1.8/timeout.rb:53:intimeout'
vendor/plugins/rspec/lib/spec/example/example_methods.rb:18:in execute'
vendor/plugins/rspec/lib/spec/example/example_group_methods.rb:303:inexecute_examples'
vendor/plugins/rspec/lib/spec/example/example_group_methods.rb:302:in each'
vendor/plugins/rspec/lib/spec/example/example_group_methods.rb:302:inexecute_examples'
vendor/plugins/rspec/lib/spec/example/example_group_methods.rb:130:in run'
vendor/plugins/rspec/lib/spec/runner/example_group_runner.rb:22:inrun'
vendor/plugins/rspec/lib/spec/runner/example_group_runner.rb:21:in each'
vendor/plugins/rspec/lib/spec/runner/example_group_runner.rb:21:inrun'
vendor/plugins/rspec/lib/spec/runner/options.rb:106:in run_examples'
vendor/plugins/rspec/lib/spec/runner/command_line.rb:19:inrun'script/spec:4

Comments and changes to this ticket

  • Will Bryant

    Will Bryant November 25th, 2008 @ 03:45 AM

    As I understand it this is by design - we want the reusable connection to start off in the same state each time.

    Where are you setting your extra connection settings at the moment?

    I ask because most obvious places I can think of would only initialise the connection settings for the first connection the app has; but it can reconnect at any time (eg. if the db server has dropped the connection due to inactivity). Do we need some kind of after_connection callback that could be used to set up project-specific goodness?

  • Pratik

    Pratik March 10th, 2009 @ 12:24 PM

    • Assigned user set to “Pratik”
    • State changed from “new” to “invalid”

    You should be using Connection's checkout callback. Does that solve the problem ?

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Pages