1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
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
214
215
216
217
218
219
220
221
222
223
224
225
|
require 'monitor'
require 'set'
module ActiveRecord
module ConnectionAdapters
class ConnectionPool
delegate :verification_timeout, :to => "::ActiveRecord::Base"
attr_reader :active_connections, :spec
def initialize(spec)
# The thread id -> adapter cache.
@active_connections = {}
# The ConnectionSpecification for this pool
@spec = spec
# The mutex used to synchronize pool access
@connection_mutex = Monitor.new
end
def active_connection_name #:nodoc:
Thread.current.object_id
end
def active_connection
active_connections[active_connection_name]
end
# Returns the connection currently associated with the class. This can
# also be used to "borrow" the connection to do database work unrelated
# to any of the specific Active Records.
def connection
if conn = active_connections[active_connection_name]
conn
else
# retrieve_connection sets the cache key.
conn = retrieve_connection
active_connections[active_connection_name] = conn
end
end
# Clears the cache which maps classes to connections.
def clear_active_connections!
clear_entries!(@active_connections, [active_connection_name]) do |name, conn|
conn.disconnect!
end
end
# Clears the cache which maps classes
def clear_reloadable_connections!
@active_connections.each do |name, conn|
if conn.requires_reloading?
conn.disconnect!
@active_connections.delete(name)
end
end
end
# Verify active connections.
def verify_active_connections! #:nodoc:
remove_stale_cached_threads!(@active_connections) do |name, conn|
conn.disconnect!
end
active_connections.each_value do |connection|
connection.verify!(verification_timeout)
end
end
def retrieve_connection #:nodoc:
# Name is nil if establish_connection hasn't been called for
# some class along the inheritance chain up to AR::Base yet.
name = active_connection_name
if conn = active_connections[name]
# Verify the connection.
conn.verify!(verification_timeout)
else
self.set_connection spec
conn = active_connections[name]
end
conn or raise ConnectionNotEstablished
end
# Returns true if a connection that's accessible to this class has already been opened.
def connected?
active_connections[active_connection_name] ? true : false
end
# Disconnect all connections in the pool.
def disconnect!
clear_cache!(@active_connections) do |name, conn|
conn.disconnect!
end
end
# Set the connection for the class.
def set_connection(spec) #:nodoc:
if spec.kind_of?(ActiveRecord::ConnectionAdapters::AbstractAdapter)
active_connections[active_connection_name] = spec
elsif spec.kind_of?(ActiveRecord::Base::ConnectionSpecification)
config = spec.config.reverse_merge(:allow_concurrency => ActiveRecord::Base.allow_concurrency)
self.set_connection ActiveRecord::Base.send(spec.adapter_method, config)
else
raise ConnectionNotEstablished
end
end
synchronize :active_connection, :connection, :clear_active_connections!,
:clear_reloadable_connections!, :verify_active_connections!, :retrieve_connection,
:connected?, :disconnect!, :set_connection, :with => :@connection_mutex
private
def clear_cache!(cache, &block)
cache.each(&block) if block_given?
cache.clear
end
# Remove stale threads from the cache.
def remove_stale_cached_threads!(cache, &block)
stale = Set.new(cache.keys)
Thread.list.each do |thread|
stale.delete(thread.object_id) if thread.alive?
end
clear_entries!(cache, stale, &block)
end
def clear_entries!(cache, keys, &block)
keys.each do |key|
next unless cache.has_key?(key)
block.call(key, cache[key])
cache.delete(key)
end
end
end
class ConnectionHandler
attr_reader :connection_pools_lock
def initialize
@connection_pools = {}
@connection_pools_lock = Monitor.new
end
def establish_connection(name, spec)
connection_pools_lock.synchronize do
@connection_pools[name] = ConnectionAdapters::ConnectionPool.new(spec)
end
end
# for internal use only and for testing
def active_connections #:nodoc:
@connection_pools.inject({}) do |hash,kv|
hash[kv.first] = kv.last.active_connection
hash.delete(kv.first) unless hash[kv.first]
hash
end
end
# Clears the cache which maps classes to connections.
def clear_active_connections!
@connection_pools.each_value {|pool| pool.clear_active_connections! }
end
# Clears the cache which maps classes
def clear_reloadable_connections!
@connection_pools.each_value {|pool| pool.clear_reloadable_connections! }
end
def clear_all_connections!
clear_cache!(@connection_pools) {|name, pool| pool.disconnect! }
end
# Verify active connections.
def verify_active_connections! #:nodoc:
@connection_pools.each_value {|pool| pool.verify_active_connections!}
end
# Locate the connection of the nearest super class. This can be an
# active or defined connection: if it is the latter, it will be
# opened and set as the active connection for the class it was defined
# for (not necessarily the current class).
def retrieve_connection(klass) #:nodoc:
pool = retrieve_connection_pool(klass)
(pool && pool.connection) or raise ConnectionNotEstablished
end
def retrieve_connection_pool(klass)
loop do
pool = @connection_pools[klass.name]
return pool if pool
return nil if ActiveRecord::Base == klass
klass = klass.superclass
end
end
# Returns true if a connection that's accessible to this class has already been opened.
def connected?(klass)
retrieve_connection_pool(klass).connected?
end
# Remove the connection for this class. This will close the active
# connection and the defined connection (if they exist). The result
# can be used as an argument for establish_connection, for easily
# re-establishing the connection.
def remove_connection(klass)
pool = @connection_pools[klass.name]
@connection_pools.delete_if { |key, value| value == pool }
pool.disconnect! if pool
pool.spec.config if pool
end
synchronize :retrieve_connection, :retrieve_connection_pool, :connected?,
:remove_connection, :active_connections, :clear_active_connections!,
:clear_reloadable_connections!, :clear_all_connections!,
:verify_active_connections!, :with => :connection_pools_lock
private
def clear_cache!(cache, &block)
cache.each(&block) if block_given?
cache.clear
end
end
end
end
|