module AdvancedConnection::ActiveRecordExt::ConnectionPool::WithoutConnection

Constants

MAX_REAQUIRE_ATTEMPTS

Public Instance Methods

without_connection(&block) click to toggle source
# File lib/advanced_connection/active_record_ext/connection_pool/without_connection.rb, line 39
def without_connection(&block)
  return unless block_given?

  # if we're not enabled just execute the block and return
  return block.call unless AdvancedConnection.enable_without_connection

  # if we have a transaction open, we can't release the database connection
  # or Bad Things (tm) happen - so we just execute our block and return
  if transaction_open?
    Rails.logger.warn "WithoutConnection skipped due to open transaction."
    return block.call
  end

  if AdvancedConnection.callbacks.without_connection.present?
    run_callbacks(:without_connection) do
      __without_connection { block.call }
    end
  else
    __without_connection { block.call }
  end
end

Private Instance Methods

__without_connection() { || ... } click to toggle source
# File lib/advanced_connection/active_record_ext/connection_pool/without_connection.rb, line 63
def __without_connection
  # return the connection to the pool for the duration of `yield`
  release_connection if active_connection?
  raise Error::UnableToReleaseConnection if active_connection?
  yield
ensure
  attempt = 0
  begin
    # attempt to retrieve another connection
    retrieve_connection
  rescue ActiveRecord::ConnectionTimeoutError
    if attempt >= MAX_REAQUIRE_ATTEMPTS
      Rails.logger.info "Giving up on trying to reacquire database connection"
      raise Error::UnableToReaquireConnection
    else
      Rails.logger.warn "Failed to reaquire database connection - reattempt #{attempt += 1}/#{MAX_REAQUIRE_ATTEMPTS} ..."
    end

    retry
  end
end
after_without_connection() click to toggle source
# File lib/advanced_connection/active_record_ext/connection_pool/without_connection.rb, line 97
def after_without_connection
  callbacks = AdvancedConnection.callbacks.without_connection
  callbacks.after.call if callbacks.after.respond_to? :call
end
around_without_connection() { || ... } click to toggle source
# File lib/advanced_connection/active_record_ext/connection_pool/without_connection.rb, line 85
def around_without_connection
  callbacks = AdvancedConnection.callbacks.without_connection
  callbacks.around.call do
    yield
  end if callbacks.around.respond_to? :call
end
before_without_connection() click to toggle source
# File lib/advanced_connection/active_record_ext/connection_pool/without_connection.rb, line 92
def before_without_connection
  callbacks = AdvancedConnection.callbacks.without_connection
  callbacks.before.call if callbacks.before.respond_to? :call
end