Skip to content

Commit c468f3c

Browse files
committed
Add allow_retry kwarg to execute
This extends the existing class level auto_retry functionality to optionally allow an allow_retry boolean kwarg to allow retry of specific queries. This PR preserves the requirement that `autocommit?` be enabled which is the safer choice. ``` def with_retry(allow_retry: false) # :nodoc: should_retry = (allow_retry || self.class.auto_retry?) && autocommit? ``` See issue rsim#2310
1 parent 7500049 commit c468f3c

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

Diff for: lib/active_record/connection_adapters/oracle_enhanced/database_statements.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ module DatabaseStatements
99
# see: abstract/database_statements.rb
1010

1111
# Executes a SQL statement
12-
def execute(sql, name = nil, async: false)
12+
def execute(sql, name = nil, async: false, allow_retry: false)
1313
sql = transform_query(sql)
1414

15-
log(sql, name, async: async) { @raw_connection.exec(sql) }
15+
log(sql, name, async: async) { @raw_connection.exec(sql, allow_retry: allow_retry) }
1616
end
1717

1818
def exec_query(sql, name = "SQL", binds = [], prepare: false, async: false)

Diff for: lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,12 @@ def reset!
9797
raise OracleEnhanced::ConnectionException, e.message
9898
end
9999

100-
def exec(sql, *bindvars, &block)
101-
@raw_connection.exec(sql, *bindvars, &block)
100+
def exec(sql, *bindvars, allow_retry: false, &block)
101+
with_retry(allow_retry: allow_retry) { @raw_connection.exec(sql, *bindvars, &block) }
102102
end
103103

104-
def with_retry(&block)
105-
@raw_connection.with_retry(&block)
104+
def with_retry(allow_retry: false, &block)
105+
@raw_connection.with_retry(allow_retry: allow_retry, &block)
106106
end
107107

108108
def prepare(sql)
@@ -435,8 +435,8 @@ def reset! # :nodoc:
435435
LOST_CONNECTION_ERROR_CODES = [ 28, 1012, 3113, 3114, 3135 ] # :nodoc:
436436

437437
# Adds auto-recovery functionality.
438-
def with_retry # :nodoc:
439-
should_retry = self.class.auto_retry? && autocommit?
438+
def with_retry(allow_retry: false) # :nodoc:
439+
should_retry = (allow_retry || self.class.auto_retry?) && autocommit?
440440

441441
begin
442442
yield

Diff for: spec/active_record/connection_adapters/oracle_enhanced/connection_spec.rb

+5
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,11 @@ def kill_current_session
461461
expect(@conn.exec("SELECT * FROM dual")).not_to be_nil
462462
end
463463

464+
it "should reconnect and execute SQL statement if connection is lost and allow_retry is passed" do
465+
kill_current_session
466+
expect(@conn.exec("SELECT * FROM dual", allow_retry: true)).not_to be_nil
467+
end
468+
464469
it "should not reconnect and execute SQL statement if connection is lost and auto retry is disabled" do
465470
# @conn.auto_retry = false
466471
ActiveRecord::Base.connection.auto_retry = false

0 commit comments

Comments
 (0)