Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #428: sqlserver_reconnect.patch

File sqlserver_reconnect.patch, 2.6 kB (added by kajism@yahoo.com, 3 years ago)

Implementation of active? and reconnect! for sqlserver_adapter.rb

  • sqlserver_adapter.rb

    old new  
    3030      if mode == "ODBC" 
    3131        raise ArgumentError, "Missing DSN. Argument ':dsn' must be set in order for this adapter to work." unless config.has_key?(:dsn) 
    3232        dsn       = config[:dsn] 
    33         conn      = DBI.connect("DBI:ODBC:#{dsn}", username, password) 
     33        driver_url = "DBI:ODBC:#{dsn}" 
    3434      else 
    3535        raise ArgumentError, "Missing Database. Argument ':database' must be set in order for this adapter to work." unless config.has_key?(:database) 
    3636        database  = config[:database] 
    3737        host      = config[:host] ? config[:host].to_s : 'localhost' 
    38         conn      = DBI.connect("DBI:ADO:Provider=SQLOLEDB;Data Source=#{host};Initial Catalog=#{database};User Id=#{username};Password=#{password};") 
     38        driver_url = "DBI:ADO:Provider=SQLOLEDB;Data Source=#{host};Initial Catalog=#{database};User Id=#{username};Password=#{password};" 
    3939      end 
     40      conn      = DBI.connect(driver_url, username, password) 
    4041 
    4142      conn["AutoCommit"] = true 
    42       ConnectionAdapters::SQLServerAdapter.new(conn, logger
     43      ConnectionAdapters::SQLServerAdapter.new(conn, logger, [driver_url, username, password]
    4344    end 
    4445  end # class Base 
    4546 
     
    172173    # unixODBC 2.2.11, Ruby ODBC 0.996, Ruby DBI 0.0.23 and Ruby 1.8.2. 
    173174    # [Linux strongmad 2.6.11-1.1369_FC4 #1 Thu Jun 2 22:55:56 EDT 2005 i686 i686 i386 GNU/Linux] 
    174175    class SQLServerAdapter < AbstractAdapter 
     176     
     177      def initialize(connection, logger, connection_options=nil) 
     178        super(connection, logger) 
     179        @connection_options = connection_options 
     180      end 
     181 
    175182      def native_database_types 
    176183        { 
    177184          :primary_key => "int NOT NULL IDENTITY(1, 1) PRIMARY KEY", 
     
    196203        true 
    197204      end 
    198205 
     206      # CONNECTION MANAGEMENT ====================================# 
     207 
     208      # Returns true if the connection is active. 
     209      def active? 
     210        @connection.execute("SELECT 1") {|sth|} 
     211        true 
     212      rescue DBI::DatabaseError => e 
     213        false 
     214      end 
     215 
     216      # Reconnects to the database. 
     217      def reconnect! 
     218        begin 
     219          @connection.disconnect 
     220          @connection = DBI.connect(*@connection_options) 
     221        rescue DBI::DatabaseError => e 
     222          @logger.warn "#{adapter_name} automatic reconnection failed: #{e.message}" 
     223        end 
     224      end 
     225 
     226 
     227 
    199228      def select_all(sql, name = nil) 
    200229        select(sql, name) 
    201230      end