aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2005-11-15 11:35:14 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2005-11-15 11:35:14 +0000
commite7b65b590a386537fa17898aaba47d816c1ea215 (patch)
tree908e928fb5c5603be487c3b0e34b5eeaf353a8ea
parent3441dfdb12d229ef07246a68ce1a775d77adf808 (diff)
downloadrails-e7b65b590a386537fa17898aaba47d816c1ea215.tar.gz
rails-e7b65b590a386537fa17898aaba47d816c1ea215.tar.bz2
rails-e7b65b590a386537fa17898aaba47d816c1ea215.zip
SQLServer: active? and reconnect! methods for handling stale connections. References #428.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3044 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r--activerecord/CHANGELOG2
-rw-r--r--activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb35
2 files changed, 34 insertions, 3 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index dedbd26675..55ba45904e 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -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>]
diff --git a/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb
index ec85c88c61..78486a2a1a 100644
--- a/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb
@@ -30,16 +30,17 @@ module ActiveRecord
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 @@ module ActiveRecord
# 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 @@ module ActiveRecord
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