Skip to content

Instantly share code, notes, and snippets.

@kuroneko
Created April 2, 2014 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuroneko/9927088 to your computer and use it in GitHub Desktop.
Save kuroneko/9927088 to your computer and use it in GitHub Desktop.
Safeish Retryable Transactions for RoR
module TransactionHelper
extend ActiveSupport::Concern
included do
def retryable_transaction(isolation = :serializable, &block)
txn_retries = 0
begin
self.transaction(isolation: isolation, &block)
rescue ::ActiveRecord::StatementInvalid => exc
if exc.original_exception.is_a?(::PG::TransactionRollback)
txn_retries += 1
retry if txn_retries <= Settings.max_txn_retries
end
raise
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment