Skip to content

Commit

Permalink
Cause #execute_fails_if_the_connection_is_closed to pass under MySQL 5.7
Browse files Browse the repository at this point in the history
  • Loading branch information
lorint committed Jan 14, 2023
1 parent bc22c89 commit ba2d4dc
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions lib/trilogy_adapter/backwards_ar_compatibility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,18 @@ def initialize(version_string, full_version_string = nil)
def with_raw_connection(allow_retry: false, uses_transaction: true)
@lock.synchronize do
@raw_connection = @connection || nil unless instance_variable_defined?(:@raw_connection)
verify! unless @verified # || (@raw_connection&.closed? == false && (@raw_connection.server_status & 1).positive?)
unless @verified
verify!
@verified = true
end
materialize_transactions if uses_transaction
begin
yield @raw_connection
rescue StandardError => exception
@verified = false unless exception.is_a?(Deadlocked) || exception.is_a?(LockWaitTimeout)
@verified = false unless exception.is_a?(Deadlocked) || exception.is_a?(LockWaitTimeout) ||
# Timed out while in a transaction
((@raw_connection.server_status & 1).positive? &&
exception.is_a?(Errno::ETIMEDOUT))
raise
end
end
Expand Down Expand Up @@ -155,7 +161,6 @@ def exec_rollback_db_transaction
alias raw_execute execute
def execute(sql, name = nil, **kwargs)
@raw_connection = nil unless instance_variable_defined?(:@raw_connection)
# Was: (!@verified && !active?)
reconnect if @raw_connection.nil? || (!@verified && (@raw_connection.server_status & 16384).zero?)
if default_timezone == :local
@raw_connection.query_flags |= ::Trilogy::QUERY_FLAGS_LOCAL_TIMEZONE
Expand All @@ -166,19 +171,17 @@ def execute(sql, name = nil, **kwargs)
rescue => exception
return if exception.is_a?(Deadlocked)

@verified = false unless exception.is_a?(LockWaitTimeout)
puts @raw_connection.server_status
@verified = false unless exception.is_a?(LockWaitTimeout) ||
((@raw_connection.server_status & 1).positive? &&
exception.cause.is_a?(Errno::ETIMEDOUT))
raise
end
else # For ActiveRecord 7.0
def execute(sql, name = nil, **kwargs)
sql = transform_query(sql)
check_if_write_query(sql)
begin
super
rescue => exception
@verified = true if exception.is_a?(ActiveRecord::StatementInvalid) && (@raw_connection.server_status & 16384).positive?
raise
end
super
end
end

Expand Down

0 comments on commit ba2d4dc

Please sign in to comment.