module Protobuf
  module ActiveRecord
    module ServiceFilters
      extend ::ActiveSupport::Concern

      included do
        around_filter :_protobuf_active_record_with_connection
      end

      def _protobuf_active_record_with_connection
        ::ActiveRecord::Base.connection_pool.with_connection do
          yield
        end
      end
    end
  end
end

module Protobuf
  module ActiveRecord
    module ServiceFilters
      extend ::ActiveSupport::Concern

      included do
        after_filter :_protobuf_active_record_clear_active_connections
      end

      def _protobuf_active_record_clear_active_connections
        ::ActiveRecord::Base.clear_active_connections!
      end
    end
  end
end

Use clear_active_connections! instead of with_connection
Use clear_active_connections! instead of with_connection

with_connection only clears a single connection. If any additional
connections are created or used by a thread, they won't be cleared.
Instead, use clear_active_connections! in an after filter, which is what
Rails does after requests.
