title: ActiveRecord execute SQL need to close connection? DEPRECATION WARNING: Database connections will not be closed automatically
body: <p>First off, I am using Rails 3.2 on Puma (but with MRI), and am not doing any explicit threading by hand.</p>
  
  <p>I am in places using the <code>execute</code> method, like <code>MyModel.connection.execute</code> or I know <code>ActiveRecord::Base.connection.execute</code> can do the same thing since all connections are to the same DB for me right now.</p>
  
  <p>I recently started seeing</p>
  
  <pre><code>DEPRECATION WARNING: Database connections will not be closed automatically, please close your database connection at the end of the thread by calling `close` on your connection. For example ActiveRecord::Base.connection.close
  </code></pre>
  
  <p>This seems self-explanatory, but I can find very little information on it online, mostly about using ActiveRecord with Sinatra (ex <a href="https://stackoverflow.com/questions/10191531/activerecord-connection-warning-database-connections-will-not-be-closed-automa">ActiveRecord connection warning. (Database connections will not be closed automatically)</a>).</p>
  
  <p>I read this:</p>
  
  <p><a href="http://blog.daniel-azuma.com/archives/216" rel="nofollow noreferrer">http://blog.daniel-azuma.com/archives/216</a></p>
  
  <p>which suggests that Rack middleware does it for me as long as the DB execute transaction is done in a controller (if I understand correctly). Does this mean transactions done elsewhere (such as a model, or decorator - they're useful in many places, so I don't want to put them right in a single controller) must be explicitly closed? Even if it's a model method that does it which was called from a controller, ex:</p>
  
  <pre><code>class MyController
    def show
      MyModel.do_execute_sql_stuff
    end
  end
  
  class MyModel
    def self.do_execute_sql_stuff
      connection.execute("WHATEVER;")
    end
  end
  </code></pre>
  
  <p>Do I need to explicitly close here? If so, should I use <code>MyModel.connection.close</code> or <code>MyModel.clear_active_connections!</code> as the article suggests? Maybe because English is not my first language, but that method sounds dangerous! And how do I go do it?</p>
  
  <pre><code>conn = MyModel.connection
  result = conn.execute("STUFF")
  do_stuff_with(result)
  conn.close |or| MyModel.clear_active_connections!
  </code></pre>
  
  <p>Like that?</p>
  
  <p>What about when using <code>find_by_sql</code>? Will it return the connection to the pool, or do I have to explicitly do that?</p>
  
  <p>EDIT: Strangely, I only see this in my production logs. Not on development, nor staging (which should be identical to production).</p>