aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
Commit message (Collapse)AuthorAgeFilesLines
* Ensure test threads share a DB connectioneileencodes2017-02-201-1/+11
| | | | | | | | | | | | | | | | | | | | | | | This ensures multiple threads inside a transactional test to see consistent database state. When a system test starts Puma spins up one thread and Capybara spins up another thread. Because of this when tests are run the database cannot see what was inserted into the database on teardown. This is because there are two threads using two different connections. This change uses the statement cache to lock the threads to using a single connection ID instead of each not being able to see each other. This code only runs in the fixture setup and teardown so it does not affect real production databases. When a transaction is opened we set `lock_thread` to `Thread.current` so we can keep track of which connection the thread is using. When we rollback the transaction we unlock the thread and then there will be no left-over data in the database because the transaction will roll back the correct connections. [ Eileen M. Uchitelle, Matthew Draper ]
* Fix pool_from_any_process to use most recent speceileencodes2017-01-131-1/+1
| | | | | | | | | | | | | If a process is forked more than once, the pool was grabbing the oldest spec, not the most recent spec. This wasn't noticed before because most folks are lilely forking the process only once. If you're forking the process multiple times however the wrong spec name will be returned and an incorrect connection will be used for the process. This fixes the issue by reversing the list of spec names so we can grab the most recent spec rather than the oldest spec.
* Clear available connections immediatelyMatthew Draper2016-11-271-0/+2
| | | | | | | | | | It'll be re-cleared when it's rebuilt in with_new_connections_blocked's ensure, but we still need to clear it inside this synchronize -- we've disconnected connections that may be available in the queue, and while other threads are not allowed to make *new* connections, they are still allowed to take existing ones from there. This was incorrectly removed in d314646c965b045724e6bdb9d61dcecfabc0ba8f.
* Distribute connections to previously blocked threads when we're doneMatthew Draper2016-11-251-19/+17
| | | | | | Two methods block new connections; we were already doing the right thing for clear_reloadable_connections, but it's better placed in with_new_connections_blocked, where it can work for disconnect too.
* Merge pull request #27057 from kamipo/fix_race_conditionMatthew Draper2016-11-191-6/+7
|\ | | | | | | Fix the race condition caused by `with_new_connections_blocked`
| * Fix the race condition caused by `with_new_connections_blocked`Ryuta Kamizono2016-11-181-8/+4
|/ | | | | | `with_new_connections_blocked` was introduced at #14938. But the method sometimes causes `@new_cons_enabled = false` then never toggled to true.
* Merge pull request #26905 from bogdanvlviv/docsAndrew White2016-11-131-6/+6
|\ | | | | Add missing `+` around a some literals.
| * Add missing `+` around a some literals.bogdanvlviv2016-10-271-6/+6
| | | | | | | | | | | | Mainly around `nil` [ci skip]
* | Merge pull request #26978 from matthewd/query-cache-poolMatthew Draper2016-11-101-0/+1
|\ \ | | | | | | Configure query caching (per thread) on the connection pool
| * | Configure query caching (per thread) on the connection poolMatthew Draper2016-11-061-0/+1
| | |
* | | Add ActiveRecord::Base.connection_pool.statPavel2016-11-091-0/+18
|/ /
* / Add more rubocop rules about whitespacesRafael Mendonça França2016-10-291-1/+1
|/
* Fix broken comments indentation caused by rubocop auto-correct [ci skip]Ryuta Kamizono2016-09-141-42/+42
| | | | | | All indentation was normalized by rubocop auto-correct at 80e66cc4d90bf8c15d1a5f6e3152e90147f00772. But comments was still kept absolute position. This commit aligns comments with method definitions for consistency.
* Add three new rubocop rulesRafael Mendonça França2016-08-161-2/+2
| | | | | | | | Style/SpaceBeforeBlockBraces Style/SpaceInsideBlockBraces Style/SpaceInsideHashLiteralBraces Fix all violations in the repository.
* normalizes indentation and whitespace across the projectXavier Noria2016-08-061-180/+180
|
* modernizes hash syntax in activerecordXavier Noria2016-08-061-3/+3
|
* applies new string literal convention in activerecord/libXavier Noria2016-08-061-5/+5
| | | | | The current code base is not uniform. After some discussion, we have chosen to go with double quotes by default.
* Remove extra connection pool creationArthur Neves2016-08-021-1/+0
|
* Merge pull request #25869 from kamipo/specificiation_id_was_renamed_to_spec_nameRafael França2016-07-201-1/+1
|\ | | | | `specificiation_id` was renamed to `spec_name`
| * `specificiation_id` was renamed to `spec_name`Ryuta Kamizono2016-07-181-1/+1
| |
* | Fix `payload[:class_name]` to `payload[:spec_name]`Ryuta Kamizono2016-07-171-1/+1
|/ | | | | | Follow up to #20818. `retrieve_connection` is passed `spec_name` instead of `klass` since #24844.
* Create connection.active_record notification and use that to ensure that lazy-Jeremy Wadsack2016-07-121-0/+15
| | | | | | | | | | | | | | | | | | | loaded model classes have their connections wrapped in transactions. See #17776 In Rails 4 config.eager_load was changed to false in the test environment. This means that model classes that connect to alternate databases with establish_connection are not loaded at start up. If use_transactional_fixtures is enabled, transactions are wrapped around the connections that have been established only at the start of the test suite. So model classes loaded later don't have transactions causing data created in the alternate database not to be removed. This change resolves that by creating a new connection.active_record notification that gets fired whenever a connection is established. I then added a subscriber after we set up transactions in the test environment to listen for additional connections and wrap those in transactions as well.
* Merge pull request #25707 from matthewd/double-reapMatthew Draper2016-07-071-9/+15
|\ | | | | Don't reap connections that have already been reassigned
| * Make connection stealing more explicitMatthew Draper2016-07-061-4/+9
| |
| * Reduce locking by taking ownership of stale connectionsMatthew Draper2016-07-061-9/+8
| | | | | | | | | | | | This way, we aren't racing other threads, so we don't need to re-check the conditional. And we no longer need to hold the lock while calling remove (which can choose to make a new connection while we wait).
| * Re-check that the connection is still stale before we reap itMatthew Draper2016-07-061-0/+2
| | | | | | | | | | | | | | A concurrent thread may have also detected it to be stale, and already released (or even reassigned) it by now. Fixes #25585
* | [ci skip] Update ConnectionPool documentation for readabilityAlex Kitchens2016-07-061-21/+21
|/
* Add to_hash to specificationArthur Neves2016-05-251-2/+1
|
* Remove `name` from `establish_connection`Arthur Neves2016-05-241-10/+7
| | | | | Instead of passing a separete name variable, we can make the resolver merge a name on the config, and use that before creating the Specification.
* Move establish_connection to handlerArthur Neves2016-05-241-1/+9
|
* [ci skip] Update retrieve_connection_pool commentBenjamin Quorning2016-05-201-7/+1
| | | | | | | | | After PR https://github.com/rails/rails/pull/24844 the documentation for `#retrieve_connection_pool` was out of date. This commit changes: - the reference from `@class_to_pool` to `@owner_to_pool`. - with newer Rubies, `#fetch` isn't significantly slower than `#[]`. Since Rails 5 requires Ruby >= 2.2.2, we can just use `#fetch` here.
* [ci skip] Update documents of `ConnectionHandler`yui-knk2016-05-111-3/+1
| | | | | | | | Follow up of #24844. The key of `@owner_to_pool` was changed from `klass.name` to `spec.name`. By this change "memory leaks in development mode" will not happen, bacause the equality of string is not changed by reloading of model files.
* Replacement cycle for readabilityMolchanov Andrey2016-05-091-2/+2
|
* Followup to #24844Jon Moss2016-05-071-0/+2
| | | | | | | Some slight documentation edits and fixes. Also, run remove unnecessary `RuntimeError`. r? @arthurnn
* Update docs for connection handlerArthur Neves2016-05-061-2/+5
| | | | [skip ci]
* s/specification_id/specification_nameArthur Neves2016-05-051-16/+16
|
* inline retrive_conn_pool methodArthur Neves2016-05-051-12/+8
|
* Refactor connection handlerArthur Neves2016-05-051-37/+20
| | | | | | | | | | | | | | | ConnectionHandler will not have any knowlodge of AR models now, it will only know about the specs. Like that we can decouple the two, and allow the same model to use more than one connection. Historically, folks used to create abstract AR classes on the fly in order to have multiple connections for the same model, and override the connection methods. With this, now we can override the `specificiation_id` method in the model, to return a key, that will be used to find the connection_pool from the handler.
* Publish AS::Executor and AS::Reloader APIsMatthew Draper2016-03-021-19/+0
| | | | | | These should allow external code to run blocks of user code to do "work", at a similar unit size to a web request, without needing to get intimate with ActionDipatch.
* Explain the connection pool error message betterDmytrii Nagirniak2015-12-011-1/+1
| | | | | | | | | | | | | The previous message was misleading (especially for Ops guys) when diagnosing problems related to the database connection. The message was suggesting that the connection cannot be obtained which normally assumes the need to look at the database. But this isn't the case as the connection could not be retrieved from the application's internal connection pool. The new message should make it more explicit and remove the confusion.
* Avoids mutating the original response in connection management middlewareKevin Buchanan2015-11-061-4/+3
|
* Require only necessary concurrent-ruby classes.Jerry D'Antonio2015-11-041-1/+1
|
* applies new doc guidelines to Active Record.Yves Senn2015-10-141-16/+20
| | | | | | | | | | | | | | | | | | | The focus of this change is to make the API more accessible. References to method and classes should be linked to make it easy to navigate around. This patch makes exzessiv use of `rdoc-ref:` to provide more readable docs. This makes it possible to document `ActiveRecord::Base#save` even though the method is within a separate module `ActiveRecord::Persistence`. The goal here is to bring the API closer to the actual code that you would write. This commit only deals with Active Record. The other gems will be updated accordingly but in different commits. The pass through Active Record is not completely finished yet. A follow up commit will change the spots I haven't yet had the time to update. /cc @fxn
* fix RDoc markup in `ConnectionPool`. [ci skip]Yves Senn2015-10-141-13/+13
|
* Replaced `ThreadSafe::Map` with successor `Concurrent::Map`.Jerry D'Antonio2015-09-191-6/+6
| | | | | | | The thread_safe gem is being deprecated and all its code has been merged into the concurrent-ruby gem. The new class, Concurrent::Map, is exactly the same as its predecessor except for fixes to two bugs discovered during the merge.
* Merge pull request #21637 from amitsuroliya/doc_fixesKasper Timm Hansen2015-09-171-2/+2
|\ | | | | Improved ActiveRecord Connection Pool docs [ci skip]
| * Improved ActiveRecord Connection Pool docs [ci skip]amitkumarsuroliya2015-09-171-2/+2
| |
* | s/Github/GitHub/Akira Matsuda2015-09-171-1/+1
|/ | | | [ci skip]
* Revert "Revert "Reduce allocations when running AR callbacks.""Guo Xiang Tan2015-07-161-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This reverts commit bdc1d329d4eea823d07cf010064bd19c07099ff3. Before: Calculating ------------------------------------- 22.000 i/100ms ------------------------------------------------- 229.700 (± 0.4%) i/s - 1.166k Total Allocated Object: 9939 After: Calculating ------------------------------------- 24.000 i/100ms ------------------------------------------------- 246.443 (± 0.8%) i/s - 1.248k Total Allocated Object: 7939 ``` begin require 'bundler/inline' rescue LoadError => e $stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler' raise e end gemfile(true) do source 'https://rubygems.org' # gem 'rails', github: 'rails/rails', ref: 'bdc1d329d4eea823d07cf010064bd19c07099ff3' gem 'rails', github: 'rails/rails', ref: 'd2876141d08341ec67cf6a11a073d1acfb920de7' gem 'arel', github: 'rails/arel' gem 'sqlite3' gem 'benchmark-ips' end require 'active_record' require 'benchmark/ips' ActiveRecord::Base.establish_connection('sqlite3::memory:') ActiveRecord::Migration.verbose = false ActiveRecord::Schema.define do create_table :users, force: true do |t| t.string :name, :email t.boolean :admin t.timestamps null: false end end class User < ActiveRecord::Base default_scope { where(admin: true) } end admin = true 1000.times do attributes = { name: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.", email: "foobar@email.com", admin: admin } User.create!(attributes) admin = !admin end GC.disable Benchmark.ips(5, 3) do |x| x.report { User.all.to_a } end key = if RUBY_VERSION < '2.2' :total_allocated_object else :total_allocated_objects end before = GC.stat[key] User.all.to_a after = GC.stat[key] puts "Total Allocated Object: #{after - before}" ```
* Add schema cache to new connection pool after forkEugene Kenny2015-05-171-1/+3
| | | | | | | | | | | | | | Active Record detects when the process has forked and automatically creates a new connection pool to avoid sharing file descriptors. If the existing connection pool had a schema cache associated with it, the new pool should copy it to avoid unnecessarily querying the database for its schema. The code to detect that the process has forked is in ConnectionHandler, but the existing test for it was in the ConnectionManagement test file. I moved it to the right place while I was writing the new test for this change.