diff --git a/lib/active_record/connection_adapters/oracle_enhanced/database_statements.rb b/lib/active_record/connection_adapters/oracle_enhanced/database_statements.rb index 22f2d2d0c..981713318 100644 --- a/lib/active_record/connection_adapters/oracle_enhanced/database_statements.rb +++ b/lib/active_record/connection_adapters/oracle_enhanced/database_statements.rb @@ -9,10 +9,10 @@ module DatabaseStatements # see: abstract/database_statements.rb # Executes a SQL statement - def execute(sql, name = nil, async: false) + def execute(sql, name = nil, async: false, allow_retry: false) sql = transform_query(sql) - log(sql, name, async: async) { _connection.exec(sql) } + log(sql, name, async: async) { _connection.exec(sql, allow_retry: allow_retry) } end def exec_query(sql, name = "SQL", binds = [], prepare: false, async: false) diff --git a/lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb b/lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb index 36ca52b4a..329536ae6 100644 --- a/lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb +++ b/lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb @@ -97,12 +97,12 @@ def reset! raise OracleEnhanced::ConnectionException, e.message end - def exec(sql, *bindvars, &block) - @raw_connection.exec(sql, *bindvars, &block) + def exec(sql, *bindvars, allow_retry: false, &block) + with_retry(allow_retry: allow_retry) { @raw_connection.exec(sql, *bindvars, &block) } end - def with_retry(&block) - @raw_connection.with_retry(&block) + def with_retry(allow_retry: false, &block) + @raw_connection.with_retry(allow_retry: allow_retry, &block) end def prepare(sql) @@ -435,8 +435,8 @@ def reset! # :nodoc: LOST_CONNECTION_ERROR_CODES = [ 28, 1012, 3113, 3114, 3135 ] # :nodoc: # Adds auto-recovery functionality. - def with_retry # :nodoc: - should_retry = self.class.auto_retry? && autocommit? + def with_retry(allow_retry: false) # :nodoc: + should_retry = (allow_retry || self.class.auto_retry?) && autocommit? begin yield diff --git a/spec/active_record/connection_adapters/oracle_enhanced/connection_spec.rb b/spec/active_record/connection_adapters/oracle_enhanced/connection_spec.rb index 490d90a87..abf24baea 100644 --- a/spec/active_record/connection_adapters/oracle_enhanced/connection_spec.rb +++ b/spec/active_record/connection_adapters/oracle_enhanced/connection_spec.rb @@ -461,6 +461,11 @@ def kill_current_session expect(@conn.exec("SELECT * FROM dual")).not_to be_nil end + it "should reconnect and execute SQL statement if connection is lost and allow_retry is passed" do + kill_current_session + expect(@conn.exec("SELECT * FROM dual", allow_retry: true)).not_to be_nil + end + it "should not reconnect and execute SQL statement if connection is lost and auto retry is disabled" do # @conn.auto_retry = false ActiveRecord::Base.connection.auto_retry = false