@@ -48,7 +48,8 @@ def perform_query(raw_connection, sql, binds, type_casted_binds, prepare:, notif
48
48
# made since we established the connection
49
49
raw_connection . query_options [ :database_timezone ] = default_timezone
50
50
51
- result = if binds . nil? || binds . empty?
51
+ result = nil
52
+ if binds . nil? || binds . empty?
52
53
ActiveSupport ::Dependencies . interlock . permit_concurrent_loads do
53
54
result = raw_connection . query ( sql )
54
55
# Ref: https://github.com/brianmario/mysql2/pull/1383
@@ -57,39 +58,40 @@ def perform_query(raw_connection, sql, binds, type_casted_binds, prepare:, notif
57
58
# By avoiding to call `#affected_rows` when we have a result, we reduce the likeliness
58
59
# of hitting the bug.
59
60
@affected_rows_before_warnings = result &.size || raw_connection . affected_rows
60
- result
61
61
end
62
- result
63
- else
64
- if prepare
65
- stmt = @statements [ sql ] ||= raw_connection . prepare ( sql )
66
- else
67
- stmt = raw_connection . prepare ( sql )
62
+ elsif prepare
63
+ stmt = @statements [ sql ] ||= raw_connection . prepare ( sql )
64
+ begin
65
+ ActiveSupport ::Dependencies . interlock . permit_concurrent_loads do
66
+ result = stmt . execute ( *type_casted_binds )
67
+ @affected_rows_before_warnings = stmt . affected_rows
68
+ end
69
+ rescue ::Mysql2 ::Error
70
+ @statements . delete ( sql )
71
+ raise
68
72
end
73
+ else
74
+ stmt = raw_connection . prepare ( sql )
69
75
70
76
begin
71
77
ActiveSupport ::Dependencies . interlock . permit_concurrent_loads do
72
78
result = stmt . execute ( *type_casted_binds )
73
79
@affected_rows_before_warnings = stmt . affected_rows
74
-
75
- # Ref: https://github.com/brianmario/mysql2/pull/1383
76
- # by eagerly closing uncached prepared statements, we also reduce the chances of
77
- # that bug happening. It can still happen if `#execute` is used as we have no callback
78
- # to eagerly close the statement.
79
- result . instance_variable_set ( :@_ar_stmt_to_close , stmt ) if result && !prepare
80
- result
81
80
end
82
- rescue ::Mysql2 ::Error
83
- if prepare
84
- @statements . delete ( sql )
81
+
82
+ # Ref: https://github.com/brianmario/mysql2/pull/1383
83
+ # by eagerly closing uncached prepared statements, we also reduce the chances of
84
+ # that bug happening. It can still happen if `#execute` is used as we have no callback
85
+ # to eagerly close the statement.
86
+ if result
87
+ result . instance_variable_set ( :@_ar_stmt_to_close , stmt )
85
88
else
86
89
stmt . close
87
90
end
88
-
91
+ rescue ::Mysql2 ::Error
92
+ stmt . close
89
93
raise
90
94
end
91
-
92
- result
93
95
end
94
96
95
97
notification_payload [ :affected_rows ] = @affected_rows_before_warnings
0 commit comments