Class: ActiveRecord::ConnectionAdapters::DB2QueryConnection
- Inherits:
-
Object
- Object
- ActiveRecord::ConnectionAdapters::DB2QueryConnection
show all
- Includes:
- ActiveSupport::Callbacks, DB2Query::DatabaseStatements
- Defined in:
- lib/active_record/connection_adapters/db2_query_adapter.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#active? ⇒ Boolean
-
#check_version ⇒ Object
-
#connect ⇒ Object
-
#disconnect! ⇒ Object
-
#enable_lazy_transactions! ⇒ Object
-
#expire ⇒ Object
-
#initialize(connector, config) ⇒ DB2QueryConnection
constructor
A new instance of DB2QueryConnection.
-
#lease ⇒ Object
-
#log(sql, name = "SQL", binds = [], type_casted_binds = [], statement_name = nil) ⇒ Object
-
#reconnect! ⇒ Object
(also: #reset!)
-
#seconds_idle ⇒ Object
-
#steal! ⇒ Object
-
#translate_exception(exception, message:, sql:, binds:) ⇒ Object
-
#translate_exception_class(e, sql, binds) ⇒ Object
-
#verify! ⇒ Object
#exec_query, #execute, #query, #query_rows, #query_value, #query_values
Constructor Details
Returns a new instance of DB2QueryConnection.
31
32
33
34
35
36
37
38
|
# File 'lib/active_record/connection_adapters/db2_query_adapter.rb', line 31
def initialize(connector, config)
@connector = connector
@instrumenter = ActiveSupport::Notifications.instrumenter
@config = config
@pool = ActiveRecord::ConnectionAdapters::NullPool.new
@lock = ActiveSupport::Concurrency::LoadInterlockAwareMonitor.new
connect
end
|
Instance Attribute Details
#connector ⇒ Object
Returns the value of attribute connector.
28
29
30
|
# File 'lib/active_record/connection_adapters/db2_query_adapter.rb', line 28
def connector
@connector
end
|
#lock ⇒ Object
Returns the value of attribute lock.
28
29
30
|
# File 'lib/active_record/connection_adapters/db2_query_adapter.rb', line 28
def lock
@lock
end
|
#owner ⇒ Object
Also known as:
in_use?
Returns the value of attribute owner.
28
29
30
|
# File 'lib/active_record/connection_adapters/db2_query_adapter.rb', line 28
def owner
@owner
end
|
#pool ⇒ Object
Returns the value of attribute pool.
27
28
29
|
# File 'lib/active_record/connection_adapters/db2_query_adapter.rb', line 27
def pool
@pool
end
|
Instance Method Details
#active? ⇒ Boolean
45
46
47
|
# File 'lib/active_record/connection_adapters/db2_query_adapter.rb', line 45
def active?
@connection.connected?
end
|
#check_version ⇒ Object
62
63
|
# File 'lib/active_record/connection_adapters/db2_query_adapter.rb', line 62
def check_version
end
|
#connect ⇒ Object
40
41
42
43
|
# File 'lib/active_record/connection_adapters/db2_query_adapter.rb', line 40
def connect
@connection = connector.connect
@connection.use_time = true
end
|
#disconnect! ⇒ Object
55
56
57
58
59
60
|
# File 'lib/active_record/connection_adapters/db2_query_adapter.rb', line 55
def disconnect!
if @connection.connected?
@connection.commit
@connection.disconnect
end
end
|
#enable_lazy_transactions! ⇒ Object
65
66
67
|
# File 'lib/active_record/connection_adapters/db2_query_adapter.rb', line 65
def enable_lazy_transactions!
@lazy_transactions_enabled = true
end
|
#expire ⇒ Object
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/active_record/connection_adapters/db2_query_adapter.rb', line 125
def expire
if in_use?
if @owner != Thread.current
raise ActiveRecordError, "Cannot expire connection, " \
"it is owned by a different thread: #{@owner}. " \
"Current thread: #{Thread.current}."
end
@idle_since = Concurrent.monotonic_time
@owner = nil
else
raise ActiveRecordError, "Cannot expire connection, it is not currently leased."
end
end
|
#lease ⇒ Object
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/active_record/connection_adapters/db2_query_adapter.rb', line 69
def lease
if in_use?
msg = +"Cannot lease connection, "
if @owner == Thread.current
msg << "it is already leased by the current thread."
else
msg << "it is already in use by a different thread: #{@owner}. " \
"Current thread: #{Thread.current}."
end
raise ActiveRecordError, msg
end
@owner = Thread.current
end
|
#log(sql, name = "SQL", binds = [], type_casted_binds = [], statement_name = nil) ⇒ Object
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/active_record/connection_adapters/db2_query_adapter.rb', line 98
def log(sql, name = "SQL", binds = [], type_casted_binds = [], statement_name = nil) @instrumenter.instrument(
"sql.active_record",
sql: sql,
name: name,
binds: binds,
type_casted_binds: type_casted_binds,
statement_name: statement_name,
connection_id: object_id,
connection: self) do
@lock.synchronize do
yield
end
rescue => e
raise translate_exception_class(e, sql, binds)
end
end
|
#reconnect! ⇒ Object
Also known as:
reset!
49
50
51
52
|
# File 'lib/active_record/connection_adapters/db2_query_adapter.rb', line 49
def reconnect!
disconnect!
connect
end
|
#seconds_idle ⇒ Object
152
153
154
155
|
# File 'lib/active_record/connection_adapters/db2_query_adapter.rb', line 152
def seconds_idle return 0 if in_use?
Concurrent.monotonic_time - @idle_since
end
|
#steal! ⇒ Object
140
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/active_record/connection_adapters/db2_query_adapter.rb', line 140
def steal!
if in_use?
if @owner != Thread.current
pool.send :remove_connection_from_thread_cache, self, @owner
@owner = Thread.current
end
else
raise ActiveRecordError, "Cannot steal connection, it is not currently leased."
end
end
|
#translate_exception(exception, message:, sql:, binds:) ⇒ Object
116
117
118
119
120
121
122
123
|
# File 'lib/active_record/connection_adapters/db2_query_adapter.rb', line 116
def translate_exception(exception, message:, sql:, binds:)
case exception
when RuntimeError
exception
else
ActiveRecord::StatementInvalid.new(message, sql: sql, binds: binds)
end
end
|
#translate_exception_class(e, sql, binds) ⇒ Object
88
89
90
91
92
93
94
95
96
|
# File 'lib/active_record/connection_adapters/db2_query_adapter.rb', line 88
def translate_exception_class(e, sql, binds)
message = "#{e.class.name}: #{e.message}"
exception = translate_exception(
e, message: message, sql: sql, binds: binds
)
exception.set_backtrace e.backtrace
exception
end
|
#verify! ⇒ Object
84
85
86
|
# File 'lib/active_record/connection_adapters/db2_query_adapter.rb', line 84
def verify!
reconnect! unless active?
end
|