|
| 1 | +require 'db/mysql' |
| 2 | + |
| 3 | +# Regression tests for the MySQL/MariaDB connection-lost translation, the |
| 4 | +# MySQL analog of the PostgreSQL backend-disconnect patch. |
| 5 | +# |
| 6 | +# A JDBCError whose SQLState (class 08), vendor error code, message, or wrapped |
| 7 | +# Java exception indicates the server connection is gone must translate to |
| 8 | +# ActiveRecord::ConnectionFailed so AR's with_raw_connection(allow_retry:) |
| 9 | +# machinery will reconnect and retry. Without this, a proxy (e.g. ProxySQL) or |
| 10 | +# the server dropping an idle connection surfaces as a raw JDBCError / |
| 11 | +# StatementInvalid and the safe retry never triggers. |
| 12 | +class MySQLConnectionLostTest < Test::Unit::TestCase |
| 13 | + |
| 14 | + def setup |
| 15 | + @adapter = ActiveRecord::Base.connection |
| 16 | + end |
| 17 | + |
| 18 | + # https://dev.mysql.com/doc/connector-j/en/connector-j-reference-error-sqlstates.html |
| 19 | + # Class 08 - Connection Exception (08S01 = "Communications link failure"). |
| 20 | + CONNECTION_FAILURE_SQL_STATES = %w[ |
| 21 | + 08000 |
| 22 | + 08001 |
| 23 | + 08003 |
| 24 | + 08004 |
| 25 | + 08006 |
| 26 | + 08007 |
| 27 | + 08S01 |
| 28 | + ] |
| 29 | + |
| 30 | + # CR_SERVER_GONE_ERROR, CR_SERVER_LOST, ER_SERVER_SHUTDOWN, |
| 31 | + # ER_CONNECTION_KILLED, ER_CLIENT_INTERACTION_TIMEOUT. |
| 32 | + CONNECTION_FAILURE_ERROR_CODES = [2006, 2013, 1053, 1927, 4031] |
| 33 | + |
| 34 | + CONNECTION_FAILURE_MESSAGES = [ |
| 35 | + 'Communications link failure', |
| 36 | + 'No operations allowed after connection closed', |
| 37 | + 'Connection refused', |
| 38 | + 'Could not connect to address=(host=localhost)(port=3306)', |
| 39 | + 'Server shutdown in progress', |
| 40 | + 'Connection is closed', |
| 41 | + ] |
| 42 | + |
| 43 | + CONNECTION_FAILURE_SQL_STATES.each do |state| |
| 44 | + define_method("test_translates_sqlstate_#{state}_to_connection_failed") do |
| 45 | + err = jdbc_error('boom', sql_state: state) |
| 46 | + result = translate(err) |
| 47 | + assert_kind_of ActiveRecord::ConnectionFailed, result, |
| 48 | + "expected SQLState #{state} to translate to ConnectionFailed, got #{result.class}" |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + CONNECTION_FAILURE_ERROR_CODES.each do |code| |
| 53 | + define_method("test_translates_error_code_#{code}_to_connection_failed") do |
| 54 | + err = jdbc_error('boom', error_code: code) |
| 55 | + result = translate(err) |
| 56 | + assert_kind_of ActiveRecord::ConnectionFailed, result, |
| 57 | + "expected error code #{code} to translate to ConnectionFailed, got #{result.class}" |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + CONNECTION_FAILURE_MESSAGES.each_with_index do |msg, i| |
| 62 | + define_method("test_translates_message_#{i}_to_connection_failed") do |
| 63 | + err = jdbc_error(msg) |
| 64 | + result = translate(err) |
| 65 | + assert_kind_of ActiveRecord::ConnectionFailed, result, |
| 66 | + "expected message #{msg.inspect} to translate to ConnectionFailed, got #{result.class}" |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + def test_recoverable_jdbc_exception_translates_to_connection_failed |
| 71 | + cause = Java::JavaSql::SQLRecoverableException.new('socket gone') |
| 72 | + err = ActiveRecord::JDBCError.new('socket gone', cause) |
| 73 | + assert_kind_of ActiveRecord::ConnectionFailed, translate(err) |
| 74 | + end |
| 75 | + |
| 76 | + def test_non_transient_connection_exception_translates_to_connection_failed |
| 77 | + cause = Java::JavaSql::SQLNonTransientConnectionException.new('link down') |
| 78 | + err = ActiveRecord::JDBCError.new('link down', cause) |
| 79 | + assert_kind_of ActiveRecord::ConnectionFailed, translate(err) |
| 80 | + end |
| 81 | + |
| 82 | + def test_does_not_translate_duplicate_entry_to_connection_failed |
| 83 | + # ER_DUP_ENTRY (1062) is a data error, not a connection failure. |
| 84 | + err = jdbc_error("Duplicate entry 'x' for key 'PRIMARY'", sql_state: '23000', error_code: 1062) |
| 85 | + result = translate(err) |
| 86 | + assert_kind_of ActiveRecord::RecordNotUnique, result |
| 87 | + assert !result.is_a?(ActiveRecord::ConnectionFailed) |
| 88 | + end |
| 89 | + |
| 90 | + def test_does_not_translate_syntax_error_to_connection_failed |
| 91 | + # ER_PARSE_ERROR (1064) must not be mistaken for a connection failure. |
| 92 | + err = jdbc_error('You have an error in your SQL syntax', sql_state: '42000', error_code: 1064) |
| 93 | + result = translate(err) |
| 94 | + assert !result.is_a?(ActiveRecord::ConnectionFailed), |
| 95 | + "syntax error should not translate to ConnectionFailed, got #{result.class}" |
| 96 | + end |
| 97 | + |
| 98 | + private |
| 99 | + |
| 100 | + def translate(jdbc_error) |
| 101 | + @adapter.send(:translate_exception_class, jdbc_error, 'SELECT 1', []) |
| 102 | + end |
| 103 | + |
| 104 | + def jdbc_error(message, sql_state: nil, error_code: 0) |
| 105 | + cause = Java::JavaSql::SQLException.new(message, sql_state, error_code) |
| 106 | + ActiveRecord::JDBCError.new(message, cause) |
| 107 | + end |
| 108 | +end |
0 commit comments