module Sequel::TransactionConnectionValidator

Public Instance Methods

transaction(opts=OPTS) click to toggle source

Rescue disconnect errors raised when beginning a new transaction. If there is a disconnnect error, it should be safe to retry the transaction using a new connection, as we haven't yielded control to the user yet.

Calls superclass method
# File lib/sequel/extensions/transaction_connection_validator.rb, line 37
def transaction(opts=OPTS)
  super
rescue DisconnectRetry => e
  if synchronize(opts[:server]){|conn| conn.equal?(e.connection)}
    # If retrying would use the same connection, that means the
    # connection was not removed from the pool, which means the caller has
    # already checked out the connection, and retrying will not be successful.
    # In this case, we can only reraise the exception.
    raise e.database_error
  end

  num_retries ||= 0 
  num_retries += 1
  retry if num_retries < 5

  raise e.database_error
end

Private Instance Methods

begin_new_transaction(conn, opts) click to toggle source

Reraise disconnect errors as DisconnectRetry so they can be retried.

Calls superclass method
# File lib/sequel/extensions/transaction_connection_validator.rb, line 58
def begin_new_transaction(conn, opts)
  super
rescue Sequel::DatabaseDisconnectError, *database_error_classes => e
  if e.is_a?(Sequel::DatabaseDisconnectError) || disconnect_error?(e, OPTS)
    exception = DisconnectRetry.new(e.message)
    exception.set_backtrace([])
    exception.connection = conn
    unless e.is_a?(Sequel::DatabaseError)
      e = Sequel.convert_exception_class(e, database_error_class(e, OPTS))
    end
    exception.database_error = e
    raise exception
  end

  raise
end