Skip to content
Commits on Mar 1, 2016
  1. @matthewd

    Publish AS::Executor and AS::Reloader APIs

    matthewd committed Feb 22, 2016
    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.
Commits on Nov 30, 2015
  1. Explain the connection pool error message better

    Dmytrii Nagirniak committed Dec 1, 2015
    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.
Commits on Nov 6, 2015
  1. @kevinbuch
Commits on Nov 5, 2015
  1. @jdantonio
Commits on Oct 14, 2015
  1. @senny

    applies new doc guidelines to Active Record.

    senny committed Jul 8, 2015
    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
  2. @senny
Commits on Sep 19, 2015
  1. @jdantonio

    Replaced `ThreadSafe::Map` with successor `Concurrent::Map`.

    jdantonio committed Sep 19, 2015
    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.
Commits on Sep 17, 2015
  1. @kaspth

    Merge pull request #21637 from amitsuroliya/doc_fixes

    kaspth committed Sep 17, 2015
    Improved ActiveRecord Connection Pool docs [ci skip]
  2. @amitsuroliya
Commits on Sep 16, 2015
  1. @amatsuda


    amatsuda committed Sep 17, 2015
    [ci skip]
Commits on Jul 15, 2015
  1. @tgxworld

    Revert "Revert "Reduce allocations when running AR callbacks.""

    tgxworld committed Jul 15, 2015
    This reverts commit bdc1d32.
    Calculating -------------------------------------
                            22.000  i/100ms
                            229.700  (± 0.4%) i/s -      1.166k
    Total Allocated Object: 9939
    Calculating -------------------------------------
                            24.000  i/100ms
                            246.443  (± 0.8%) i/s -      1.248k
    Total Allocated Object: 7939
      require 'bundler/inline'
    rescue LoadError => e
      $stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
      raise e
    gemfile(true) do
      source '';
      # gem 'rails', github: 'rails/rails', ref: 'bdc1d329d4eea823d07cf010064bd19c07099ff3'
      gem 'rails', github: 'rails/rails', ref: 'd2876141d08341ec67cf6a11a073d1acfb920de7'
      gem 'arel', github: 'rails/arel'
      gem 'sqlite3'
      gem 'benchmark-ips'
    require 'active_record'
    require 'benchmark/ips'
    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
    class User < ActiveRecord::Base
      default_scope { where(admin: true) }
    admin = true
    1000.times do
      attributes = {
        name: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
        email: "",
        admin: admin
      admin = !admin
    Benchmark.ips(5, 3) do |x| { User.all.to_a }
    key =
      if RUBY_VERSION < '2.2'
    before = GC.stat[key]
    after = GC.stat[key]
    puts "Total Allocated Object: #{after - before}"
Commits on May 16, 2015
  1. @eugeneius

    Add schema cache to new connection pool after fork

    eugeneius committed May 17, 2015
    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
Commits on May 14, 2015
  1. @thedarkone

    AR::ConPool - remove synchronization around connection cache.

    thedarkone committed May 14, 2015
    Renamed `@reserved_connections` -> `@thread_cached_conns`. New name
    clearly conveys the purpose of the cache, which is to speed-up
    `#connection` method.
    The new `@thread_cached_conns` now also uses `Thread` objects as keys
    (instead of previously `Thread.current.object_id`).
    Since there is no longer any synchronization around
    `@thread_cached_conns`, `disconnect!` and `clear_reloadable_connections!`
    methods now pre-emptively obtain ownership (via `checkout`) of all
    existing connections, before modifying internal data structures.
    A private method `release` has been renamed `thread_conn_uncache` to
    clear-up its purpose.
    Fixed some brittle `thread.status == "sleep"` tests (threads can go
    into sleep even without locks).
  2. @thedarkone
  3. @thedarkone

    AR::ConPool - reduce post checkout critical section.

    thedarkone committed Apr 4, 2015
    Move post checkout connection verification out of mutex.synchronize.
Commits on Apr 29, 2015
  1. @eugeneius

    Apply schema cache dump when creating connections

    eugeneius committed Apr 29, 2015
    The `db:schema:cache:dump` rake task dumps the database schema structure
    to `db/schema_cache.dump`. If this file is present, the schema details
    are loaded into the currently checked out connection by a railtie while
    Rails is booting, to avoid having to query the database for its schema.
    The schema cache dump is only applied to the initial connection used to
    boot the application though; other connections from the same pool are
    created with an empty schema cache, and still have to load the structure
    of each table directly from the database.
    With this change, a copy of the schema cache is associated with the
    connection pool and applied to connections as they are created.
Commits on Mar 22, 2015
  1. @tgxworld

    Revert "Reduce allocations when running AR callbacks."

    tgxworld committed Mar 22, 2015
    This reverts commit 796cab4.
Commits on Jan 13, 2015
  1. @yoshiokatsuneo

    ActiveRecord: On reconnection failure, release only failed connetion.

    yoshiokatsuneo committed Jan 13, 2015
    On reconnection failure, all the connection was released.
    But, it is better to release only failed connection.
    This patch changes not to release all the connection but release
    only failed connection.
Commits on Jan 9, 2015
  1. @yoshiokatsuneo

    ActiveRecord: release connection on reconnect failure.

    yoshiokatsuneo committed Jan 9, 2015
    When trying to checkout connection from connection pool,
    checkout()(and checkout_and_verify) verify whether the connection
    is active or not.
    And, if the connection is not active, connection adapters try to
    reconnect to server.  And, if database is down at this moment,
    reconnect fails and exception is raised.
    (Ex: Mysql2::Error: Can't connect to local MySQL server through socket xxx)
    But, ConnectionPool does not catch the exception, but leaks current
    disconnected connection to @connection.
    So, if database's temporary down happens several times and exceeds
    the number of connection pool(5 by default), activerecord will be
    no more available, even if database server is already recovered.
    This patch fix it by catching exception and releasing connection.
Commits on Jan 4, 2015
  1. @rafaelfranca
Commits on Jan 2, 2015
  1. @rafaelfranca

    Merge pull request #18080 from korbin/fix_reaping_frequency_configura…

    rafaelfranca committed Jan 2, 2015
    Fix issue with reaping_frequency type.
Commits on Dec 23, 2014
  1. @al2o3cr

    Fix connection leak when a thread checks in additional connections.

    al2o3cr committed Dec 23, 2014
    The code in `ConnectionPool#release` assumed that a single thread only
    ever holds a single connection, and thus that releasing a connection
    only requires the owning thread_id.
    There is a trivial counterexample to this assumption: code that checks
    out additional connections from the pool in the same thread. For
        connection_1 = ActiveRecord::Base.connection
        connection_2 = ActiveRecord::Base.connection_pool.checkout
        connection_3 = ActiveRecord::Base.connection
    At this point, connection_1 has been removed from the
    `@reserved_connections` hash, causing a NEW connection to be returned as
    connection_3 and the loss of any tracking info on connection_1. As long
    as the thread in this example lives, connection_1 will be inaccessible
    and un-reapable. If this block of code runs more times than the size of
    the connection pool in a single thread, every subsequent connection
    attempt will timeout, as all of the available connections have been
    Reverts parts of 9e457a8 and
    essentially all of 4367d2f
Commits on Dec 18, 2014
  1. @korbin

    fix issue with reaping_frequency type

    korbin committed Dec 17, 2014
    When using DATABASE_URL to configure ActiveRecord, :reaping_frequency
    does not get converted from a string to a numeric value. This value is
    eventually passed to 'sleep' and must be numeric to avoid exceptions.
    This commit converts :reaping_frequency to a float when present.
Commits on Dec 8, 2014
  1. minor sentences fixes

    Neeraj Singh committed Dec 7, 2014
Commits on Nov 29, 2014
  1. @sferik
Commits on Oct 29, 2014
  1. @fxn

    edit pass over all warnings

    fxn committed Oct 28, 2014
    This patch uniformizes warning messages. I used the most common style
    already present in the code base:
    * Capitalize the first word.
    * End the message with a full stop.
    * "Rails 5" instead of "Rails 5.0".
    * Backticks for method names and inline code.
    Also, converted a few long strings into the new heredoc convention.
Commits on Oct 28, 2014
  1. @fxn

    let's warn with heredocs

    fxn committed Oct 28, 2014
    The current style for warning messages without newlines uses
    concatenation of string literals with manual trailing spaces
    where needed.
    Heredocs have better readability, and with `squish` we can still
    produce a single line.
    This is a similar use case to the one that motivated defining
    `strip_heredoc`, heredocs are super clean.
Commits on Oct 26, 2014
  1. @rafaelfranca

    Prefix internal method with _

    rafaelfranca committed Oct 25, 2014
    This will avoid naming clash with user defined methods
Commits on Sep 28, 2014
  1. @phiggins

    Reduce allocations when running AR callbacks.

    phiggins committed Sep 28, 2014
    Inspired by @tenderlove's work in
    c363fff, this reduces the number of
    strings allocated when running callbacks for ActiveRecord instances. I
    measured that using this script:
    require 'objspace'
    require 'active_record'
    require 'allocation_tracer'
    ActiveRecord::Base.establish_connection adapter: "sqlite3",
                                            database: ":memory:"
    ActiveRecord::Base.connection.instance_eval do
      create_table(:articles) { |t| t.string :name }
    class Article < ActiveRecord::Base; end
    a = Article.create name: "foo"
    a = Article.find
    N = 10
    result = ObjectSpace::AllocationTracer.trace do
      N.times { Article.find }
    result.sort.each do |k,v|
      p k => v
    puts "total: #{}"
    When I run this against master and this branch I get this output:
    pete@balloon:~/projects/rails/activerecord$ git checkout master
    M Gemfile
    Switched to branch 'master'
    pete@balloon:~/projects/rails/activerecord$ bundle exec ruby benchmark_allocation_with_callback_send.rb > allocations_before
    pete@balloon:~/projects/rails/activerecord$ git checkout remove-dynamic-send-on-built-in-callbacks
    M Gemfile
    Switched to branch 'remove-dynamic-send-on-built-in-callbacks'
    pete@balloon:~/projects/rails/activerecord$ bundle exec ruby benchmark_allocation_with_callback_send.rb > allocations_after
    pete@balloon:~/projects/rails/activerecord$ diff allocations_before allocations_after
    81]=>[40, 0, 0, 0, 0, 0]}
    < total: 630
    > total: 590
    In addition to this, there are two micro-optimizations present:
    * Using ` if block` vs `yield if block_given?` when the block was being captured already.
    pete@balloon:~/projects$ cat benchmark_block_call_vs_yield.rb
    require 'benchmark/ips'
    def block_capture_with_yield &block
      yield if block_given?
    def block_capture_with_call &block if block
    def no_block_capture
      yield if block_given?
    Benchmark.ips do |b|"block_capture_with_yield") { block_capture_with_yield }"block_capture_with_call") { block_capture_with_call }"no_block_capture") { no_block_capture }
    pete@balloon:~/projects$ ruby benchmark_block_call_vs_yield.rb
    Calculating -------------------------------------
                            124979 i/100ms
                            138340 i/100ms
        no_block_capture    136827 i/100ms
                          5703108.9 (±2.4%) i/s -   28495212 in   4.999368s
                          6840730.5 (±3.6%) i/s -   34169980 in   5.002649s
        no_block_capture  5821141.4 (±2.8%) i/s -   29144151 in   5.010580s
    * Defining and calling methods instead of using send.
    pete@balloon:~/projects$ cat benchmark_method_call_vs_send.rb
    require 'benchmark/ips'
    class Foo
      def tacos
    my_foo =
    Benchmark.ips do |b|'send') { my_foo.send('tacos') }'call') { my_foo.tacos }
    pete@balloon:~/projects$ ruby benchmark_method_call_vs_send.rb
    Calculating -------------------------------------
                    send     97736 i/100ms
                    call    151142 i/100ms
                    send  2683730.3 (±2.8%) i/s -   13487568 in   5.029763s
                    call  8005963.9 (±2.7%) i/s -   40052630 in   5.006604s
    The result of this is making typical ActiveRecord operations slightly faster:
Commits on Sep 23, 2014
  1. @Sirupsen

    ar/connection_pool: honor overriden rack.test in middleware

    Sirupsen committed Sep 23, 2014
    Honoring an overidden `rack.test` allows testing closed connection between
    multiple requests. This is useful if you're working on database resiliency, to
    ensure the connection is in the expected state from one request to another on
    the same worker.
Commits on Aug 19, 2014
  1. @rafaelfranca

    Merge pull request #16542 from seamusabshere/numerify-pool-checkout-t…

    rafaelfranca committed Aug 18, 2014
    Make sure :checkout_timeout and :dead_connection_timeout are numbers
Commits on Jul 31, 2014
  1. @JackDanger

    Clarify how the ConnectionHandler works

    JackDanger committed Jul 30, 2014
    This makes the implicit description of how connection pooling works a
    little more explicit. It converts the examples of a model hierarchy into
    actual Ruby code and demonstrates how the key structure of the
    database.yml relates to the `establish_connection` method.
Commits on Jun 23, 2014
  1. @mmozuras
Commits on Mar 18, 2014
  1. @matthewd

    Reap connections based on owning-thread death

    matthewd committed Mar 8, 2014
    .. not a general timeout.
    Now, if a thread checks out a connection then dies, we can immediately
    recover that connection and re-use it.
    This should alleviate the pool exhaustion discussed in #12867. More
    importantly, it entirely avoids the potential issues of the reaper
    attempting to check whether connections are still active: as long as the
    owning thread is alive, the connection is its business alone.
    As a no-op reap is now trivial (only entails checking a thread status
    per connection), we can also perform one in-line any time we decide to
    sleep for a connection.
Commits on Feb 22, 2014
  1. @jeremy

    Distinguish ConnectionNotEstablished messages: no conn pool for the c…

    jeremy committed Feb 22, 2014
    …lass, or no conn available from the pool
Something went wrong with that request. Please try again.