Class: ActiveRecord::ConnectionAdapters::OracleEnhancedJDBCConnection

Inherits:
OracleEnhancedConnection show all
Defined in:
lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb

Overview

JDBC database interface for JRuby

Instance Attribute Summary collapse

Attributes inherited from OracleEnhancedConnection

#raw_connection

Instance Method Summary collapse

Methods inherited from OracleEnhancedConnection

create, #oracle_downcase

Constructor Details

#initialize(config) ⇒ OracleEnhancedJDBCConnection

Returns a new instance of OracleEnhancedJDBCConnection.



44
45
46
47
48
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 44

def initialize(config)
  @active = true
  @config = config
  new_connection(@config)
end

Instance Attribute Details

#activeObject Also known as: active?

Returns the value of attribute active.



37
38
39
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 37

def active
  @active
end

#auto_retryObject Also known as: auto_retry?

Returns the value of attribute auto_retry.



40
41
42
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 40

def auto_retry
  @auto_retry
end

Instance Method Details

#autocommit=(value) ⇒ Object



105
106
107
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 105

def autocommit=(value)
  @raw_connection.setAutoCommit(value)
end

#autocommit?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 101

def autocommit?
  @raw_connection.getAutoCommit
end

#commitObject



93
94
95
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 93

def commit
  @raw_connection.commit
end

#describe(name) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 223

def describe(name)
  real_name = OracleEnhancedAdapter.valid_table_name?(name) ? name.to_s.upcase : name.to_s
  if real_name.include?('.')
    table_owner, table_name = real_name.split('.')
  else
    table_owner, table_name = @owner, real_name
  end
  sql = <<-SQL
    SELECT owner, table_name, 'TABLE' name_type
    FROM all_tables
    WHERE owner = '#{table_owner}'
      AND table_name = '#{table_name}'
    UNION ALL
    SELECT owner, view_name table_name, 'VIEW' name_type
    FROM all_views
    WHERE owner = '#{table_owner}'
      AND view_name = '#{table_name}'
    UNION ALL
    SELECT table_owner, table_name, 'SYNONYM' name_type
    FROM all_synonyms
    WHERE owner = '#{table_owner}'
      AND synonym_name = '#{table_name}'
    UNION ALL
    SELECT table_owner, table_name, 'SYNONYM' name_type
    FROM all_synonyms
    WHERE owner = 'PUBLIC'
      AND synonym_name = '#{real_name}'
  SQL
  if result = select_one(sql)
    case result['name_type']
    when 'SYNONYM'
      describe("#{result['owner']}.#{result['table_name']}")
    else
      [result['owner'], result['table_name']]
    end
  else
    raise OracleEnhancedConnectionException, %Q{"DESC #{name}" failed; does it exist?}
  end
end

#exec(sql) ⇒ Object



155
156
157
158
159
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 155

def exec(sql)
  with_retry do
    exec_no_retry(sql)
  end
end

#exec_no_retry(sql) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 161

def exec_no_retry(sql)
  cs = @raw_connection.prepareCall(sql)
  case sql
  when /\A\s*UPDATE/i, /\A\s*INSERT/i, /\A\s*DELETE/i
    cs.executeUpdate
  else
    cs.execute
    true
  end
ensure
  cs.close rescue nil        
end

#logoffObject



85
86
87
88
89
90
91
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 85

def logoff
  @active = false
  @raw_connection.close
  true
rescue
  false
end

#new_connection(config) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 50

def new_connection(config)
  username, password, database = config[:username].to_s, config[:password].to_s, config[:database].to_s
  privilege = config[:privilege] && config[:privilege].to_s
  host, port = config[:host], config[:port]

  url = config[:url] || "jdbc:oracle:thin:@#{host || 'localhost'}:#{port || 1521}:#{database || 'XE'}"

  prefetch_rows = config[:prefetch_rows] || 100
  cursor_sharing = config[:cursor_sharing] || 'similar'

  properties = java.util.Properties.new
  properties.put("user", username)
  properties.put("password", password)
  properties.put("defaultRowPrefetch", "#{prefetch_rows}") if prefetch_rows
  properties.put("internal_logon", privilege) if privilege

  @raw_connection = java.sql.DriverManager.getConnection(url, properties)
  exec %q{alter session set nls_date_format = 'YYYY-MM-DD HH24:MI:SS'}
  exec %q{alter session set nls_timestamp_format = 'YYYY-MM-DD HH24:MI:SS'} # rescue nil
  exec "alter session set cursor_sharing = #{cursor_sharing}" # rescue nil
  self.autocommit = true
  
  # Set session time zone to current time zone
  @raw_connection.setSessionTimeZone(java.util.TimeZone.default.getID)
  
  # Set default number of rows to prefetch
  # @raw_connection.setDefaultRowPrefetch(prefetch_rows) if prefetch_rows
  
  # default schema owner
  @owner = username.upcase
  
  @raw_connection
end

#pingObject

Checks connection, returns true if active. Note that ping actively checks the connection, while #active? simply returns the last known state.



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 112

def ping
  exec_no_retry("select 1 from dual")
  @active = true
rescue NativeException => e
  @active = false
  if e.message =~ /^java\.sql\.SQLException/
    raise OracleEnhancedConnectionException, e.message
  else
    raise
  end
end

#reset!Object

Resets connection, by logging off and creating a new connection.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 125

def reset!
  logoff rescue nil
  begin
    new_connection(@config)
    @active = true
  rescue NativeException => e
    @active = false
    if e.message =~ /^java\.sql\.SQLException/
      raise OracleEnhancedConnectionException, e.message
    else
      raise
    end
  end
end

#rollbackObject



97
98
99
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 97

def rollback
  @raw_connection.rollback
end

#select(sql, name = nil, return_column_names = false) ⇒ Object



174
175
176
177
178
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 174

def select(sql, name = nil, return_column_names = false)
  with_retry do
    select_no_retry(sql, name, return_column_names)
  end        
end

#select_no_retry(sql, name = nil, return_column_names = false) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 180

def select_no_retry(sql, name = nil, return_column_names = false)
  stmt = @raw_connection.prepareStatement(sql)
  rset = stmt.executeQuery

  # Reuse the same hash for all rows
  column_hash = {}

   = rset.
  column_count = .getColumnCount
  
  cols_types_index = (1..column_count).map do |i|
    col_name = oracle_downcase(.getColumnName(i))
    next if col_name == 'raw_rnum_'
    column_hash[col_name] = nil
    [col_name, .getColumnTypeName(i).to_sym, i]
  end
  cols_types_index.delete(nil)

  rows = []
  get_lob_value = !(name == 'Writable Large Object')
  
  while rset.next
    hash = column_hash.dup
    cols_types_index.each do |col, column_type, i|
      hash[col] = get_ruby_value_from_result_set(rset, i, column_type, get_lob_value)
    end
    rows << hash
  end

  return_column_names ? [rows, cols_types_index.map(&:first)] : rows
ensure
  rset.close rescue nil
  stmt.close rescue nil
end

#with_retry(&block) ⇒ Object

mark connection as dead if connection lost



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 141

def with_retry(&block)
  should_retry = auto_retry? && autocommit?
  begin
    yield if block_given?
  rescue NativeException => e
    raise unless e.message =~ /^java\.sql\.SQLException: (Closed Connection|Io exception:|No more data to read from socket)/
    @active = false
    raise unless should_retry
    should_retry = false
    reset! rescue nil
    retry
  end
end

#write_lob(lob, value, is_binary = false) ⇒ Object



215
216
217
218
219
220
221
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 215

def write_lob(lob, value, is_binary = false)
  if is_binary
    lob.setBytes(1, value.to_java_bytes)
  else
    lob.setString(1,value)
  end
end