Skip to content

Commit 229afab

Browse files
committed
Update retry logic for ActiveRecord 6
Also, add a check so it only retries on SerializationFailure.
1 parent be413bf commit 229afab

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

lib/active_record/connection_adapters/cockroachdb/transaction_manager.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,22 @@ module TransactionManagerMonkeyPatch
88
# transactions that fail due to serialization errors. Failed
99
# transactions will be retried until they pass or the max retry limit is
1010
# exceeded.
11-
def within_new_transaction(options = {})
12-
attempts = options.fetch(:attempts, 0)
11+
def within_new_transaction(isolation: nil, joinable: true, attempts: 0)
1312
super
14-
rescue ActiveRecord::SerializationFailure => error
13+
rescue ActiveRecord::StatementInvalid => error
14+
raise unless retryable? error
1515
raise if attempts >= @connection.max_transaction_retries
1616

1717
attempts += 1
1818
sleep_seconds = (2 ** attempts + rand) / 10
1919
sleep(sleep_seconds)
20-
within_new_transaction(options.merge(attempts: attempts)) { yield }
20+
within_new_transaction(isolation: isolation, joinable: joinable, attempts: attempts) { yield }
21+
end
22+
23+
def retryable?(error)
24+
return true if error.is_a? ActiveRecord::SerializationFailure
25+
return retryable? error.cause if error.cause
26+
false
2127
end
2228
end
2329
end

0 commit comments

Comments
 (0)