class ActiveRecord::ConnectionAdapters::JdbcConnection

JDBC (connection) base class, custom adapters we support likely extend this class. For maximum performance most of this class and the sub-classes we ship are implemented in Java, check: RubyJdbcConnection.java

Attributes

adapter[R]
config[R]

Public Class Methods

new(config, adapter = nil) click to toggle source

Initializer implemented in Ruby. @note second argument is mandatory, only optional for compatibility

# File lib/arjdbc/jdbc/connection.rb, line 10
def initialize(config, adapter = nil)
  @config = config; @adapter = adapter
  @connection = nil; @jndi = nil
  # @stmts = {} # AR compatibility - statement cache not used
  setup_connection_factory
  init_connection # @see RubyJdbcConnection.init_connection
rescue Java::JavaSql::SQLException => e
  e = e.cause if defined?(NativeException) && e.is_a?(NativeException) # JRuby-1.6.8
  error = e.getMessage || e.getSQLState
  error = error ? "#{e.java_class.name}: #{error}" : e.java_class.name
  error = ::ActiveRecord::JDBCError.new("The driver encountered an unknown error: #{error}")
  error.errno = e.getErrorCode
  error.sql_exception = e
  raise error
end

Public Instance Methods

adapter=(adapter) click to toggle source

@deprecated no longer used (pass adapter into initialize) @see ActiveRecord::ConnectionAdapters::JdbcAdapter#initialize

# File lib/arjdbc/jdbc/connection.rb, line 30
def adapter=(adapter); @adapter = adapter; end
jndi?() click to toggle source
# File lib/arjdbc/jdbc/connection.rb, line 41
def jndi?; @jndi; end
Also aliased as: jndi_connection?
jndi_connection?()
Alias for: jndi?
native_database_types() click to toggle source
# File lib/arjdbc/jdbc/connection.rb, line 32
def native_database_types
  JdbcTypeConverter.new(supported_data_types).choose_best_types
end
set_native_database_types() click to toggle source

@deprecated no longer used - only kept for compatibility

# File lib/arjdbc/jdbc/connection.rb, line 37
def set_native_database_types
  ArJdbc.deprecate "set_native_database_types is no longer used and does nothing override native_database_types instead"
end
setup_connection_factory() click to toggle source

Sets the connection factory from the available configuration. @see setup_jdbc_factory @see setup_jndi_factory

@note this has nothing to do with the configure_connection implemented on some of the concrete adapters (e.g. {#ArJdbc::Postgres})

# File lib/arjdbc/jdbc/connection.rb, line 50
def setup_connection_factory
  if self.class.jndi_config?(config)
    begin
      setup_jndi_factory
    rescue => e
      warn "JNDI data source unavailable: #{e.message}; trying straight JDBC"
      setup_jdbc_factory
    end
  else
    setup_jdbc_factory
  end
end

Protected Instance Methods

setup_jdbc_factory() click to toggle source
# File lib/arjdbc/jdbc/connection.rb, line 86
def setup_jdbc_factory
  if ! config[:url] || ( ! config[:driver] && ! config[:driver_instance] )
    msg = config[:url] ? ":url = #{config[:url]}" : ":driver = #{config[:driver]}"
    raise ::ActiveRecord::ConnectionNotEstablished, "jdbc adapter requires :driver and :url (got #{msg})"
  end

  url = jdbc_url
  username = config[:username]
  password = config[:password]
  driver = ( config[:driver_instance] ||=
      JdbcDriver.new(config[:driver].to_s, config[:properties]) )

  @jndi = false
  self.connection_factory = JdbcConnectionFactoryImpl.new(url, username, password, driver)
end
setup_jndi_factory() click to toggle source
# File lib/arjdbc/jdbc/connection.rb, line 65
def setup_jndi_factory
  data_source = config[:data_source] ||
    Java::JavaxNaming::InitialContext.new.lookup(config[:jndi].to_s)

  @jndi = true
  self.connection_factory = JndiConnectionFactoryImpl.new(data_source)
end

Private Instance Methods

jdbc_url() click to toggle source
# File lib/arjdbc/jdbc/connection.rb, line 120
def jdbc_url
  url = config[:url].to_s
  if options = config[:options]
    ArJdbc.deprecate "use config[:properties] to specify connection URL properties instead of config[:options]"
    options = options.map { |key, val| "#{key}=#{val}" }.join('&') if Hash === options
    url = url['?'] ? "#{url}&#{options}" : "#{url}?#{options}" unless options.empty?
    config[:url] = url; config[:options] = nil
  end
  url
end