Created
April 2, 2014 02:39
-
-
Save kuroneko/9927088 to your computer and use it in GitHub Desktop.
Safeish Retryable Transactions for RoR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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