Skip to content

Commit 03bbb98

Browse files
committed
Merge pull request #144 from ManageIQ/add_spid
Populate the spid attribute on connect and reset it on disconnect for dblib
2 parents 5723122 + 7b79670 commit 03bbb98

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ class SQLServerAdapter < AbstractAdapter
176176
DATABASE_VERSION_REGEXP = /Microsoft SQL Server\s+"?(\d{4}|\w+)"?/
177177
SUPPORTED_VERSIONS = [2005,2008,2010,2011].freeze
178178

179-
attr_reader :database_version, :database_year
179+
attr_reader :database_version, :database_year, :spid
180180

181181
cattr_accessor :native_text_database_type, :native_binary_database_type, :native_string_database_type,
182182
:log_info_schema_queries, :enable_default_unicode_types, :auto_connect,
@@ -269,6 +269,7 @@ def reconnect!
269269
end
270270

271271
def disconnect!
272+
@spid = nil
272273
case @connection_options[:mode]
273274
when :dblib
274275
@connection.close rescue nil
@@ -399,6 +400,7 @@ def connect
399400
:encoding => encoding,
400401
:azure => config[:azure]
401402
}).tap do |client|
403+
@spid = client.execute("SELECT @@spid").first.values.first
402404
if config[:azure]
403405
client.execute("SET ANSI_NULLS ON").do
404406
client.execute("SET CURSOR_CLOSE_ON_COMMIT OFF").do

test/cases/connection_test_sqlserver.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ def setup
1111
@connection = ActiveRecord::Base.connection
1212
end
1313

14-
1514
should 'affect rows' do
1615
topic_data = { 1 => { "content" => "1 updated" }, 2 => { "content" => "2 updated" } }
1716
updated = Topic.update(topic_data.keys, topic_data.values)
@@ -93,6 +92,17 @@ def setup
9392
assert @connection.active?
9493
end
9594

95+
if connection_mode_dblib?
96+
should 'set spid on connect' do
97+
assert @connection.spid.kind_of?(Fixnum)
98+
end
99+
100+
should 'reset spid on disconnect!' do
101+
@connection.disconnect!
102+
assert @connection.spid.nil?
103+
end
104+
end
105+
96106
should 'be able to disconnect and reconnect at will' do
97107
@connection.disconnect!
98108
assert !@connection.active?

0 commit comments

Comments
 (0)