fix: AR clear statement cache after failover - #194
Merged
Merged
Conversation
After a successful failover the ActiveRecord adapters reuse the same connection object, which the wrapper has reconnected to a new physical connection, and re-run configure_connection. They did not clear ActiveRecord's prepared statement cache, so ActiveRecord kept executing statements by names that only existed on the old physical connection. The wrapper rejected each one with "Method invoked against old connection", and because the connection still looked healthy, every query prepared before the failover failed on it until the process restarted. translate_exception now calls clear_cache!(new_connection: true) before configure_connection for FailoverSuccessError and TransactionStateUnknownError, so those statements are prepared again on the new connection.
…t-cache-after-failover # Conflicts: # CHANGELOG.md
sophia-bq
approved these changes
Oct 5, 2026
JuanLeee
approved these changes
Oct 5, 2026
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
fix: clear ActiveRecord's prepared statement cache after failover
Description
In 1.0.0, after a successful failover, every query ActiveRecord had already prepared fails on that connection with
AwsAdvancedRubyDriverWrapper::Errors::AwsError: Method invoked against old connection. It keeps failing for as long as the connection lives. Queries not prepared before the failover still work.This affects:
aws_postgresql, which has prepared statements on by default. In practice, every pooled connection that survives a failover becomes unusable for its existing queries until the process restarts.aws_mysql2withprepared_statements: true, which the KMS encryption docs tell MySQL users to enable.The stock
postgresqlandmysql2adapters recover from the same connection loss without errors.Root cause:
configure_connection. They did not clear ActiveRecord's prepared statement cache.a1) that only existed on the old physical connection, and the wrapper's bounded-connection check rejected each one.AwsError, whichtranslate_exceptionreturns unchanged, and the connection still reportsactive?, so the pool keeps handing it out.Fix: for
FailoverSuccessErrorandTransactionStateUnknownError,translate_exceptionnow callsclear_cache!(new_connection: true)beforeconfigure_connection. This is ActiveRecord's own hook for a connection that has been replaced: it forgets the cached statements without trying to deallocate them on the new connection, so they are prepared again on first use. It exists with the same signature in ActiveRecord 7.2, 8.0, and 8.1.Tests:
failover_activerecord_spec.rb: a preparedfind(withprepared_statements: true) before and after a writer crash, expectingFailoverSuccessErrorand then successful repeats of the same query. The existing failover tests only ran plain SQL throughselect_value, which never uses prepared statements, so they couldn't catch this.findfailed on every attempt after theFailoverSuccessError, on both engines.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.