Sha256: aac16d1408cc30f24faeb198dadb763993cd9a68170def7ef2e2b01b6cc42533

Contents?: true

Size: 1.19 KB

Versions: 16

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

module ActiveRecord
  module ConnectionAdapters
    module CockroachDB
      module TransactionManagerMonkeyPatch
        # Capture ActiveRecord::SerializationFailure errors caused by
        # transactions that fail due to serialization errors. Failed
        # transactions will be retried until they pass or the max retry limit is
        # exceeded.
        def within_new_transaction(isolation: nil, joinable: true, attempts: 0)
          super(isolation: isolation, joinable: joinable)
        rescue ActiveRecord::StatementInvalid => error
          raise unless retryable? error
          raise if attempts >= @connection.max_transaction_retries

          attempts += 1
          sleep_seconds = (2 ** attempts + rand) / 10
          sleep(sleep_seconds)
          within_new_transaction(isolation: isolation, joinable: joinable, attempts: attempts) { yield }
        end

        def retryable?(error)
          return true if error.is_a? ActiveRecord::SerializationFailure
          return retryable? error.cause if error.cause
          false
        end
      end
    end

    class TransactionManager
      prepend CockroachDB::TransactionManagerMonkeyPatch
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
activerecord-cockroachdb-adapter-6.1.11 lib/active_record/connection_adapters/cockroachdb/transaction_manager.rb
activerecord-cockroachdb-adapter-7.0.0 lib/active_record/connection_adapters/cockroachdb/transaction_manager.rb
activerecord-cockroachdb-adapter-6.1.10 lib/active_record/connection_adapters/cockroachdb/transaction_manager.rb
activerecord-cockroachdb-adapter-6.1.9 lib/active_record/connection_adapters/cockroachdb/transaction_manager.rb
activerecord-cockroachdb-adapter-6.1.8 lib/active_record/connection_adapters/cockroachdb/transaction_manager.rb
activerecord-cockroachdb-adapter-6.1.7 lib/active_record/connection_adapters/cockroachdb/transaction_manager.rb
activerecord-cockroachdb-adapter-6.1.6 lib/active_record/connection_adapters/cockroachdb/transaction_manager.rb
activerecord-cockroachdb-adapter-6.1.5 lib/active_record/connection_adapters/cockroachdb/transaction_manager.rb
activerecord-cockroachdb-adapter-6.1.4 lib/active_record/connection_adapters/cockroachdb/transaction_manager.rb
activerecord-cockroachdb-adapter-6.1.3 lib/active_record/connection_adapters/cockroachdb/transaction_manager.rb
activerecord-cockroachdb-adapter-6.1.2 lib/active_record/connection_adapters/cockroachdb/transaction_manager.rb
activerecord-cockroachdb-adapter-6.1.1 lib/active_record/connection_adapters/cockroachdb/transaction_manager.rb
activerecord-cockroachdb-adapter-6.1.0 lib/active_record/connection_adapters/cockroachdb/transaction_manager.rb
activerecord-cockroachdb-adapter-6.1.0.pre.beta.3 lib/active_record/connection_adapters/cockroachdb/transaction_manager.rb
activerecord-cockroachdb-adapter-6.1.0.pre.beta.2 lib/active_record/connection_adapters/cockroachdb/transaction_manager.rb
activerecord-cockroachdb-adapter-6.1.0beta1 lib/active_record/connection_adapters/cockroachdb/transaction_manager.rb