aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
Commit message (Collapse)AuthorAgeFilesLines
* Merge pull request #35297 from yhara/fix-ar-connection-handler-leakRyuta Kamizono2019-02-161-7/+11
|\ | | | | | | Fix possible memory leak of ConnectionHandler
| * Fix possible memory leak of ConnectionHandlerYutaka HARA2019-02-161-7/+11
|/ | | | refs #35296
* Fix elapsed time calculationsbogdanvlviv2019-02-081-4/+4
| | | | | | | | | | | | | | | | | | | I've found a few places in Rails code base where I think it makes sense to calculate elapsed time more precisely by using `Concurrent.monotonic_time`: - Fix calculation of elapsed time in `ActiveSupport::Cache::MemoryStore#prune` - Fix calculation of elapsed time in `ActiveRecord::ConnectionAdapters::ConnectionPool::Queue#wait_poll` - Fix calculation of elapsed time in `ActiveRecord::ConnectionAdapters::ConnectionPool#attempt_to_checkout_all_existing_connections` - Fix calculation of elapsed time in `ActiveRecord::ConnectionAdapters::Mysql2Adapter#explain` See https://docs.ruby-lang.org/en/2.5.0/Process.html#method-c-clock_gettime https://blog.dnsimple.com/2018/03/elapsed-time-with-ruby-the-right-way Related to 7c4542146f0dde962205e5a90839349631ae60fb
* Fix error raised when handler doesn't existEileen Uchitelle2019-01-251-1/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | While working on another feature for multiple databases (auto-switching) I observed that in development the first request won't autoload the application record connection for the primary database and may not yet know about the replica connection. In my test application this caused the application to thrown an error if I tried to send the first request to the replica before the replica was connected. This wouldn't be an issue in production because the application is preloaded. In order to fix this I decided to leave the original error message and delete the new error message. I updated the original error message to include the `role` to make it a bit clearer that the connection isn't established for that particular role. The error now reads: ``` No connection pool with 'primary' found for the 'reading' role. ``` A single database application will continue uisng the original error message: ``` No connection pool with 'primary' found. ```
* Enable `Performance/UnfreezeString` copyuuji.yaginuma2018-09-231-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | In Ruby 2.3 or later, `String#+@` is available and `+@` is faster than `dup`. ```ruby # frozen_string_literal: true require "bundler/inline" gemfile(true) do source "https://rubygems.org" gem "benchmark-ips" end Benchmark.ips do |x| x.report('+@') { +"" } x.report('dup') { "".dup } x.compare! end ``` ``` $ ruby -v benchmark.rb ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux] Warming up -------------------------------------- +@ 282.289k i/100ms dup 187.638k i/100ms Calculating ------------------------------------- +@ 6.775M (± 3.6%) i/s - 33.875M in 5.006253s dup 3.320M (± 2.2%) i/s - 16.700M in 5.032125s Comparison: +@: 6775299.3 i/s dup: 3320400.7 i/s - 2.04x slower ```
* Merge pull request #31696 from BrentWheeldon/bmw-connection-pool-load-deadlockMatthew Draper2018-08-241-1/+3
|\ | | | | Prevent deadlocks when waiting for connection from pool.
| * Prevent deadlocks when waiting for connection from pool.Brent Wheeldon2018-03-231-1/+3
| | | | | | | | | | When a thread that had the load interlock but was blocked waiting to check a connection out of the connection pool but all of the threads using the available connections were blocked waiting to obtain the load interlock an `ActiveRecord::ConnectionTimeoutError` exception was be thrown by the thread waiting for the connection. When waiting for the connection to check out we should allow loading to proceed to avoid this deadlock.
* | `retrieve_connection_pool` return a pool, not a connectionRyuta Kamizono2018-08-031-2/+2
|/
* Merge pull request #25456 from ojab/masterRyuta Kamizono2018-01-031-3/+1
|\ | | | | | | Remove dormant check
| * Remove dormant checkojab2016-06-211-3/+1
| | | | | | | | | | This check was introduced in 6edaa26 and moved through multiple refactorings. No test are broken after removal and AFAICS there is no way to trigger it.
* | Merge pull request #31221 from matthewd/flush-idle-connectionsMatthew Draper2017-11-261-16/+58
|\ \ | | | | | | Flush idle database connections
| * | Flush idle database connectionsMatthew Draper2017-11-261-16/+58
| | |
* | | Merge pull request #31173 from matthewd/connection-fork-safetyMatthew Draper2017-11-251-0/+35
|\ \ \ | |/ / |/| | Improve AR connection fork safety
| * | Improve AR connection fork safetyMatthew Draper2017-11-181-0/+35
| | | | | | | | | | | | | | | | | | Use whatever adapter-provided means we have available to ensure forked children don't send quit/shutdown/goodbye messages to the server on connections that belonged to their parent.
* | | Merge pull request #28742 from quixoten/stack_conn_poolMatthew Draper2017-11-171-6/+3
|\ \ \ | |/ / |/| | Switch to LIFO for the connection pool
| * | Fix typosDevin Christensen2017-04-131-1/+1
| | |
| * | Improve documentation and add testDevin Christensen2017-04-131-7/+4
| | |
| * | Switch to LIFO for the connection poolDevin Christensen2017-04-121-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | Using a FIFO for the connection pool can lead to issues when there are upstream components (pgbouncer, haproxy, etc.) that terminate connections that are idle after a period of time. Switching to a LIFO reduces the probability that a thread will checkout a connection that is about to be closed by an idle timeout in an upstream component.
* | | Fix RDoc formatting: `+` doesn't work with `@`ohbarye2017-08-111-5/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | refs: https://github.com/rails/rails/pull/30161 ``` $ echo "+@size+" | rdoc --pipe <p>+@size+</p> $ echo "<tt>@size</tt>" | rdoc --pipe <p><code>@size</code></p> ``` [ci skip]
* | | Start `@reaper.run` after connection pool initializedRyuta Kamizono2017-08-111-2/+3
| | | | | | | | | | | | | | | | | | | | | Otherwise `ConnectionPool#reap` may run before `@connections` has initialized. https://travis-ci.org/rails/rails/jobs/263037427#L888-L890
* | | Use frozen-string-literal in ActiveRecordKir Shatrov2017-07-191-0/+2
| | |
* | | Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"Matthew Draper2017-07-021-1/+0
| | | | | | | | | | | | | | | This reverts commit 3420a14590c0e6915d8b6c242887f74adb4120f9, reversing changes made to afb66a5a598ce4ac74ad84b125a5abf046dcf5aa.
* | | Merge pull request #29540 from kirs/rubocop-frozen-stringMatthew Draper2017-07-021-0/+1
|\ \ \ | | | | | | | | | | | | Enforce frozen string in Rubocop
| * | | Enforce frozen string in RubocopKir Shatrov2017-07-011-0/+1
| | | |
* | | | Make ActiveRecord frozen string literal friendly.Pat Allan2017-06-201-1/+1
|/ / /
* | | Lock connection before checking it inMatthew Draper2017-04-261-6/+8
| | |
* | | Fix typos [ci skip]Tobias Fankhänel2017-04-251-2/+2
|/ / | | | | | | | | | | | | | | | | 'lookup' is the noun. 'to look up' is the verb. Looked it up just to be sure. cf. https://en.wiktionary.org/wiki/lookup https://en.wiktionary.org/wiki/look_up
* | Fix example usage of ActiveRecord::Base.establish_connectionSimon Dawson2017-03-301-1/+1
| | | | | | `ActiveRecord::Base.establish_connection` accepts a single symbol argument to specify a named connection; a single string argument appears to be interpreted as a connection URI
* | 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.