Class: ActiveRecord::ConnectionAdapters::JdbcConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/arjdbc/jdbc/connection.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, adapter = nil) ⇒ JdbcConnection

Note:

second argument is mandatory, only optional for compatibility

Initializer implemented in Ruby.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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

Instance Attribute Details

#adapterObject

Returns the value of attribute adapter.



26
27
28
# File 'lib/arjdbc/jdbc/connection.rb', line 26

def adapter
  @adapter
end

#configObject (readonly)

Returns the value of attribute config.



26
27
28
# File 'lib/arjdbc/jdbc/connection.rb', line 26

def config
  @config
end

Instance Method Details

#jndi?Boolean Also known as: jndi_connection?

Returns:

  • (Boolean)


41
# File 'lib/arjdbc/jdbc/connection.rb', line 41

def jndi?; @jndi; end

#native_database_typesObject



32
33
34
# File 'lib/arjdbc/jdbc/connection.rb', line 32

def native_database_types
  JdbcTypeConverter.new(supported_data_types).choose_best_types
end

#set_native_database_typesObject

Deprecated.

no longer used - only kept for compatibility



37
38
39
# 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_factoryObject

Note:

this has nothing to do with the configure_connection implemented

Sets the connection factory from the available configuration. on some of the concrete adapters (e.g. ActiveRecord::ConnectionAdapters::JdbcConnection#ArJdbc#ArJdbc::Postgres)



50
51
52
53
54
55
56
57
58
59
60
61
# 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

#setup_jdbc_factoryObject (protected)



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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_factoryObject (protected)



65
66
67
68
69
70
71
# 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