Class: ActiveRecord::ConnectionAdapters::OracleEnhancedJDBCConnection
Overview
JDBC database interface for JRuby
Instance Attribute Summary collapse
#raw_connection
Instance Method Summary
collapse
create, #describe, #oracle_downcase
Constructor Details
50
51
52
53
54
|
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 50
def initialize(config)
@active = true
@config = config
new_connection(@config)
end
|
Instance Attribute Details
#active ⇒ Object
Also known as:
active?
43
44
45
|
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 43
def active
@active
end
|
#auto_retry ⇒ Object
Also known as:
auto_retry?
Returns the value of attribute auto_retry.
46
47
48
|
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 46
def auto_retry
@auto_retry
end
|
Instance Method Details
#autocommit=(value) ⇒ Object
121
122
123
|
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 121
def autocommit=(value)
@raw_connection.setAutoCommit(value)
end
|
#autocommit? ⇒ Boolean
117
118
119
|
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 117
def autocommit?
@raw_connection.getAutoCommit
end
|
#commit ⇒ Object
109
110
111
|
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 109
def commit
@raw_connection.commit
end
|
#error_code(exception) ⇒ Object
Return NativeException / java.sql.SQLException error code
288
289
290
|
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 288
def error_code(exception)
exception.cause.getErrorCode
end
|
#exec(sql) ⇒ Object
171
172
173
174
175
|
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 171
def exec(sql)
with_retry do
exec_no_retry(sql)
end
end
|
#exec_no_retry(sql) ⇒ Object
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 177
def exec_no_retry(sql)
case sql
when /\A\s*(UPDATE|INSERT|DELETE)/i
s = @raw_connection.prepareStatement(sql)
s.executeUpdate
when /\A\s*(CREATE|DROP)/i
s = @raw_connection.createStatement()
s.execute(sql)
true
else
s = @raw_connection.prepareStatement(sql)
s.execute
true
end
ensure
s.close rescue nil
end
|
#exec_with_returning(sql) ⇒ Object
execute sql with RETURNING … INTO :insert_id and return :insert_id value
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 203
def exec_with_returning(sql)
with_retry do
begin
s = @raw_connection.prepareCall("BEGIN #{sql}; END;")
s.registerOutParameter(1, java.sql.Types::BIGINT)
s.execute
insert_id = s.getLong(1)
s.wasNull ? nil : insert_id
ensure
s.close rescue nil
end
end
end
|
#logoff ⇒ Object
101
102
103
104
105
106
107
|
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 101
def logoff
@active = false
@raw_connection.close
true
rescue
false
end
|
#new_connection(config) ⇒ Object
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 56
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]
if database && !host && !config[:url] && ENV['TNS_ADMIN']
url = "jdbc:oracle:thin:@#{database || 'XE'}"
else
url = config[:url] || "jdbc:oracle:thin:@#{host || 'localhost'}:#{port || 1521}:#{database || 'XE'}"
end
prefetch_rows = config[:prefetch_rows] || 100
cursor_sharing = config[:cursor_sharing] || 'force'
nls_length_semantics = config[:nls_length_semantics] || 'CHAR'
time_zone = config[:time_zone] || ENV['TZ'] || java.util.TimeZone.default.getID
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:FF6'}
exec "alter session set cursor_sharing = #{cursor_sharing}"
exec "alter session set nls_length_semantics = '#{nls_length_semantics}'"
self.autocommit = true
@raw_connection.setSessionTimeZone(time_zone)
@owner = username.upcase
@raw_connection
end
|
#ping ⇒ Object
Checks connection, returns true if active. Note that ping actively checks the connection, while #active? simply returns the last known state.
128
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 128
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.
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 141
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
|
#returning_clause(quoted_pk) ⇒ Object
197
198
199
|
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 197
def returning_clause(quoted_pk)
" RETURNING #{quoted_pk} INTO ?"
end
|
#rollback ⇒ Object
113
114
115
|
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 113
def rollback
@raw_connection.rollback
end
|
#select(sql, name = nil, return_column_names = false) ⇒ Object
238
239
240
241
242
|
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 238
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 244
def select_no_retry(sql, name = nil, return_column_names = false)
stmt = @raw_connection.prepareStatement(sql)
rset = stmt.executeQuery
column_hash = {}
metadata = rset.getMetaData
column_count = metadata.getColumnCount
cols_types_index = (1..column_count).map do |i|
col_name = oracle_downcase(metadata.getColumnName(i))
next if col_name == 'raw_rnum_'
column_hash[col_name] = nil
[col_name, metadata.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
157
158
159
160
161
162
163
164
165
166
167
168
169
|
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 157
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
279
280
281
282
283
284
285
|
# File 'lib/active_record/connection_adapters/oracle_enhanced_jdbc_connection.rb', line 279
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
|