File tree Expand file tree Collapse file tree 2 files changed +31
-3
lines changed
lib/active_record/connection_adapters Expand file tree Collapse file tree 2 files changed +31
-3
lines changed Original file line number Diff line number Diff line change @@ -677,7 +677,7 @@ def reconnect!(restore_transactions: false)
677
677
678
678
reset_transaction ( restore : restore_transactions ) do
679
679
clear_cache! ( new_connection : true )
680
- configure_connection
680
+ attempt_configure_connection
681
681
end
682
682
rescue => original_exception
683
683
translated_exception = translate_exception_class ( original_exception , nil , nil )
@@ -730,7 +730,7 @@ def discard!
730
730
def reset!
731
731
clear_cache! ( new_connection : true )
732
732
reset_transaction
733
- configure_connection
733
+ attempt_configure_connection
734
734
end
735
735
736
736
# Removes the connection from the pool and disconnect it.
@@ -766,7 +766,7 @@ def verify!
766
766
if @unconfigured_connection
767
767
@raw_connection = @unconfigured_connection
768
768
@unconfigured_connection = nil
769
- configure_connection
769
+ attempt_configure_connection
770
770
@last_activity = Process . clock_gettime ( Process ::CLOCK_MONOTONIC )
771
771
@verified = true
772
772
return
@@ -1219,6 +1219,13 @@ def configure_connection
1219
1219
check_version
1220
1220
end
1221
1221
1222
+ def attempt_configure_connection
1223
+ configure_connection
1224
+ rescue
1225
+ disconnect!
1226
+ raise
1227
+ end
1228
+
1222
1229
def default_prepared_statements
1223
1230
true
1224
1231
end
Original file line number Diff line number Diff line change @@ -873,6 +873,27 @@ def teardown
873
873
@connection . execute ( "SELECT 1" , allow_retry : true )
874
874
end
875
875
876
+ test "disconnect and recover on #configure_connection failure" do
877
+ connection = ActiveRecord ::Base . connection_pool . send ( :new_connection )
878
+
879
+ failures = [ ActiveRecord ::ConnectionFailed . new ( "Oops" ) , ActiveRecord ::ConnectionFailed . new ( "Oops 2" ) ]
880
+ connection . singleton_class . define_method ( :configure_connection ) do
881
+ if error = failures . pop
882
+ raise error
883
+ else
884
+ super ( )
885
+ end
886
+ end
887
+ assert_raises ActiveRecord ::ConnectionFailed do
888
+ connection . exec_query ( "SELECT 1" )
889
+ end
890
+
891
+ assert_equal [ [ 1 ] ] , connection . exec_query ( "SELECT 1" ) . rows
892
+ assert_empty failures
893
+ ensure
894
+ connection &.disconnect!
895
+ end
896
+
876
897
private
877
898
def raw_transaction_open? ( connection )
878
899
case connection . adapter_name
You can’t perform that action at this time.
0 commit comments