title: Heroku - ActiveRecord::ConnectionTimeoutError (could not obtain a database connection within 5.000 seconds (waited 5.000 seconds))
body: <p>I have a fairly straightforward application which is running without problem locally, but when deployed to heroku it quickly stops working with  a 500 error and the following error in the logs: </p>
  
  <pre><code>ActiveRecord::ConnectionTimeoutError (could not obtain a database connection within 5.000 seconds (waited 5.003 seconds))
  </code></pre>
  
  <p>I have had a search around and found <a href="https://stackoverflow.com/questions/21834377/solved-rails-4-custom-404-causes-postgresql-connection-failure-on-heroku">this question</a> which appears to be very similar. I don't really understand from that question what the relevance of the requests for jpeg files are, and I don't understand what the probelm is from the answer given. I can recreate by restarting the server then calling a page which returns a 404 several times.</p>
  
  <p>I have the same results as mentioned in the question (specifically Connections &lt; DB_POOL): </p>
  
  <pre><code> (staging) $ heroku pg:info --remote staging
  === HEROKU_POSTGRESQL_ONYX_URL (DATABASE_URL)
  Plan:        Hobby-dev
  Status:      Available
  Connections: 5
  PG Version:  9.3.4
  Created:     2014-05-02 08:57 UTC
  Data Size:   9.9 MB
  Tables:      11
  Rows:        6508/10000 (In compliance)
  Fork/Follow: Unsupported
  Rollback:    Unsupported
  
  (staging) $ heroku config
  .....
  DB_POOL:                    10
  .....
  </code></pre>
  
  <p>I developed my solution based on this <a href="http://railscasts.com/episodes/53-handling-exceptions-revised" rel="nofollow noreferrer">railscast</a> which seems the same approach as in the other question. I do not however understand the answer provided in this other question and how it might apply to mine solution as follows: 
   routes.rb:</p>
  
  <pre><code>match ':status', to: 'errors#show', constraints: {status: /\d{3}/ }, via: :all
  </code></pre>
  
  <p>application.rb: </p>
  
  <pre><code>config.exceptions_app = self.routes
  </code></pre>
  
  <p>errorscontroller.rb</p>
  
  <pre><code>class ErrorsController &lt; ApplicationController
  
      def show
          status = request.path[1..-1]
          case status
          when "404"
              @error = "404 - The page you were looking for cannot be found"
          when "403"
              @error = "403 - The page you were looking for is not accessible"
          when "500"
              @error = "500 - An error occurred within the server"
          end
  
  
          respond_to do |format|
              format.html { render action: "error" }
              format.json { render json: {status: status, error: @exception.message} }
          end
      end
  end
  </code></pre>
  
  <p>error.html.erb</p>
  
  <pre><code>&lt;div class='container'&gt;
      &lt;h1 class="errorh1"&gt;Oops something went wrong!!&lt;/h1&gt;
      &lt;h2 class="errorh2"&gt;&lt;%= @error %&gt;&lt;/h2&gt;
      &lt;br&gt;
      &lt;p&gt;We are sorry. Please &lt;a href='#' data-uv-trigger="contact
  "&gt;let us know&lt;/a&gt; what you were trying to do so that we can look into the problem.&lt;/p&gt;
      &lt;br&gt;
      &lt;p&gt;When you are ready to continue, &lt;a href="/"&gt;go home&lt;/a&gt; to start again.&lt;/p&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Note: I am running Rails 4.0.4 (and psql (PostgreSQL) 9.3.4 locally)</p>