Module: Sequel::TransactionConnectionValidator

Defined in:
lib/sequel/extensions/transaction_connection_validator.rb

Defined Under Namespace

Classes: DisconnectRetry

Instance Method Summary collapse

Instance Method Details

#transaction(opts = OPTS) ⇒ Object

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.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/sequel/extensions/transaction_connection_validator.rb', line 38

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