Skip to content

Commit

Permalink
Merge pull request #13 from localytics/fix-connection-management
Browse files Browse the repository at this point in the history
Fix connection management
  • Loading branch information
kddnewton authored Feb 10, 2017
2 parents a89cd29 + dab1c51 commit d34f122
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
21 changes: 9 additions & 12 deletions lib/active_record/connection_adapters/odbc_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class << self
def odbc_connection(config)
config = config.symbolize_keys

connection, options =
connection, config =
if config.key?(:dsn)
odbc_dsn_connection(config)
elsif config.key?(:conn_str)
Expand All @@ -31,7 +31,7 @@ def odbc_connection(config)
end

database_metadata = ::ODBCAdapter::DatabaseMetadata.new(connection)
database_metadata.adapter_class.new(connection, logger, database_metadata)
database_metadata.adapter_class.new(connection, logger, config, database_metadata)
end

private
Expand All @@ -40,8 +40,7 @@ def odbc_dsn_connection(config)
username = config[:username] ? config[:username].to_s : nil
password = config[:password] ? config[:password].to_s : nil
connection = ODBC.connect(config[:dsn], username, password)
options = { dsn: config[:dsn], username: username, password: password }
[connection, options]
[connection, config.merge(username: username, password: password)]
end

# Connect using ODBC connection string
Expand All @@ -61,8 +60,7 @@ def odbc_conn_str_connection(config)
end

connection = ODBC::Database.new.drvconnect(driver)
options = { conn_str: config[:conn_str], driver: driver }
[connection, options]
[connection, config.merge(driver: driver)]
end
end
end
Expand All @@ -83,9 +81,8 @@ class ODBCAdapter < AbstractAdapter

attr_reader :database_metadata

def initialize(connection, logger, database_metadata)
super(connection, logger)
@connection = connection
def initialize(connection, logger, config, database_metadata)
super(connection, logger, config)
@database_metadata = database_metadata
end

Expand Down Expand Up @@ -115,10 +112,10 @@ def active?
def reconnect!
disconnect!
@connection =
if options.key?(:dsn)
ODBC.connect(options[:dsn], options[:username], options[:password])
if @config.key?(:dsn)
ODBC.connect(@config[:dsn], @config[:username], @config[:password])
else
ODBC::Database.new.drvconnect(options[:driver])
ODBC::Database.new.drvconnect(@config[:driver])
end
super
end
Expand Down
24 changes: 24 additions & 0 deletions test/connection_management_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'test_helper'

class ConnectionManagementTest < Minitest::Test
def test_connection_management
assert conn.active?

conn.disconnect!
refute conn.active?

conn.disconnect!
refute conn.active?

conn.reconnect!
assert conn.active?
ensure
conn.reconnect!
end

private

def conn
ActiveRecord::Base.connection
end
end

0 comments on commit d34f122

Please sign in to comment.