Permalink
Browse files

SQLServer: active? and reconnect! methods for handling stale connecti…

…ons. References #428.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3044 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 3441dfd commit e7b65b590a386537fa17898aaba47d816c1ea215 @jeremy jeremy committed Nov 15, 2005
Showing with 34 additions and 3 deletions.
  1. +2 −0 activerecord/CHANGELOG
  2. +32 −3 activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb
View
@@ -1,5 +1,7 @@
*SVN*
+* SQLServer: active? and reconnect! methods for handling stale connections. #428 [kajism@yahoo.com]
+
* Associations handle case-equality more consistently: item.parts.is_a?(Array) and item.parts === Array. #1345 [MarkusQ@reality.com]
* SQLServer: insert uses given primary key value if not nil rather than SELECT @@IDENTITY. #2866 [kajism@yahoo.com, Tom Ward <tom@popdog.net>]
@@ -30,16 +30,17 @@ def self.sqlserver_connection(config) #:nodoc:
if mode == "ODBC"
raise ArgumentError, "Missing DSN. Argument ':dsn' must be set in order for this adapter to work." unless config.has_key?(:dsn)
dsn = config[:dsn]
- conn = DBI.connect("DBI:ODBC:#{dsn}", username, password)
+ driver_url = "DBI:ODBC:#{dsn}"
else
raise ArgumentError, "Missing Database. Argument ':database' must be set in order for this adapter to work." unless config.has_key?(:database)
database = config[:database]
host = config[:host] ? config[:host].to_s : 'localhost'
- conn = DBI.connect("DBI:ADO:Provider=SQLOLEDB;Data Source=#{host};Initial Catalog=#{database};User Id=#{username};Password=#{password};")
+ driver_url = "DBI:ADO:Provider=SQLOLEDB;Data Source=#{host};Initial Catalog=#{database};User Id=#{username};Password=#{password};"
end
+ conn = DBI.connect(driver_url, username, password)
conn["AutoCommit"] = true
- ConnectionAdapters::SQLServerAdapter.new(conn, logger)
+ ConnectionAdapters::SQLServerAdapter.new(conn, logger, [driver_url, username, password])
end
end # class Base
@@ -172,6 +173,12 @@ def self.binary_to_string(value)
# unixODBC 2.2.11, Ruby ODBC 0.996, Ruby DBI 0.0.23 and Ruby 1.8.2.
# [Linux strongmad 2.6.11-1.1369_FC4 #1 Thu Jun 2 22:55:56 EDT 2005 i686 i686 i386 GNU/Linux]
class SQLServerAdapter < AbstractAdapter
+
+ def initialize(connection, logger, connection_options=nil)
+ super(connection, logger)
+ @connection_options = connection_options
+ end
+
def native_database_types
{
:primary_key => "int NOT NULL IDENTITY(1, 1) PRIMARY KEY",
@@ -196,6 +203,28 @@ def supports_migrations? #:nodoc:
true
end
+ # CONNECTION MANAGEMENT ====================================#
+
+ # Returns true if the connection is active.
+ def active?
+ @connection.execute("SELECT 1") {|sth|}
+ true
+ rescue DBI::DatabaseError => e
+ false
+ end
+
+ # Reconnects to the database.
+ def reconnect!
+ begin
+ @connection.disconnect
+ @connection = DBI.connect(*@connection_options)
+ rescue DBI::DatabaseError => e
+ @logger.warn "#{adapter_name} automatic reconnection failed: #{e.message}"
+ end
+ end
+
+
+
def select_all(sql, name = nil)
select(sql, name)
end

0 comments on commit e7b65b5

Please sign in to comment.