@@ -167,9 +167,7 @@ def execute_procedure(proc_name, *variables)
167167 log ( sql , name ) do
168168 case @connection_options [ :mode ]
169169 when :dblib
170- raise ActiveRecord ::ConnectionNotEstablished if @connection . nil?
171-
172- result = @connection . execute ( sql )
170+ result = ensure_established_connection! { @connection . execute ( sql ) }
173171 options = { as : :hash , cache_rows : true , timezone : ActiveRecord ::Base . default_timezone || :utc }
174172 result . each ( options ) do |row |
175173 r = row . with_indifferent_access
@@ -359,9 +357,7 @@ def sp_executesql_sql(sql, types, params, name)
359357 def raw_connection_do ( sql )
360358 case @connection_options [ :mode ]
361359 when :dblib
362- raise ActiveRecord ::ConnectionNotEstablished if @connection . nil?
363-
364- result = @connection . execute ( sql )
360+ result = ensure_established_connection! { @connection . execute ( sql ) }
365361
366362 # TinyTDS returns false instead of raising an exception if connection fails.
367363 # Getting around this by raising an exception ourselves while this PR
@@ -432,9 +428,7 @@ def _raw_select(sql, options = {})
432428 def raw_connection_run ( sql )
433429 case @connection_options [ :mode ]
434430 when :dblib
435- raise ActiveRecord ::ConnectionNotEstablished if @connection . nil?
436-
437- @connection . execute ( sql )
431+ ensure_established_connection! { @connection . execute ( sql ) }
438432 end
439433 end
440434
@@ -468,6 +462,12 @@ def finish_statement_handle(handle)
468462 end
469463 handle
470464 end
465+
466+ def ensure_established_connection!
467+ raise ActiveRecord ::ConnectionNotEstablished , 'SQL Server client is not connected' unless @connection
468+
469+ yield
470+ end
471471 end
472472 end
473473 end
0 commit comments