Skip to content
This repository
Newer
Older
100644 442 lines (385 sloc) 15.301 kb
d8692985 » raggi
2010-11-25 Don't depend on rubygems loading thread (for Mutex)
1 require 'thread'
ff97e9d0 » nicksieger
2008-06-07 Connection handling methods extracted out into separate ConnectionHan…
2 require 'monitor'
50cd4bdc » nicksieger
2008-04-19 Introduce synchronization around connection pool access
3 require 'set'
e8550ee0 » jeremy
2009-05-13 Cherry-pick core extensions
4 require 'active_support/core_ext/module/synchronization'
bd2f5c06 » tenderlove
2011-11-19 pushing caching and visitors down to the connection
5 require 'active_support/core_ext/module/deprecation'
50cd4bdc » nicksieger
2008-04-19 Introduce synchronization around connection pool access
6
6edaa267 » nicksieger
2008-04-19 Initial conversion to connection pool
7 module ActiveRecord
fe575dd4 » nicksieger
2008-08-07 Nearing the finish line. Initial fixed-size connection pool implement…
8 # Raised when a connection could not be obtained within the connection
9 # acquisition timeout period.
10 class ConnectionTimeoutError < ConnectionNotEstablished
11 end
12
6edaa267 » nicksieger
2008-04-19 Initial conversion to connection pool
13 module ConnectionAdapters
f17159b0 » fxn
2010-06-14 edit pass: the names of Rails components have a space, ie, "Active Re…
14 # Connection pool base class for managing Active Record database
fe575dd4 » nicksieger
2008-08-07 Nearing the finish line. Initial fixed-size connection pool implement…
15 # connections.
16 #
a2932784 » lifo
2008-10-05 Merge docrails
17 # == Introduction
18 #
19 # A connection pool synchronizes thread access to a limited number of
20 # database connections. The basic idea is that each thread checks out a
21 # database connection from the pool, uses that connection, and checks the
22 # connection back in. ConnectionPool is completely thread-safe, and will
23 # ensure that a connection cannot be used by two threads at the same time,
24 # as long as ConnectionPool's contract is correctly followed. It will also
25 # handle cases in which there are more threads than connections: if all
26 # connections have been checked out, and a thread tries to checkout a
27 # connection anyway, then ConnectionPool will wait until some other thread
28 # has checked in a connection.
29 #
30 # == Obtaining (checking out) a connection
31 #
fe575dd4 » nicksieger
2008-08-07 Nearing the finish line. Initial fixed-size connection pool implement…
32 # Connections can be obtained and used from a connection pool in several
33 # ways:
34 #
f17159b0 » fxn
2010-06-14 edit pass: the names of Rails components have a space, ie, "Active Re…
35 # 1. Simply use ActiveRecord::Base.connection as with Active Record 2.1 and
817a07b4 » nicksieger
2008-08-07 More doco and class/method renames. Now have a strategy for integrati…
36 # earlier (pre-connection-pooling). Eventually, when you're done with
37 # the connection(s) and wish it to be returned to the pool, you call
38 # ActiveRecord::Base.clear_active_connections!. This will be the
f17159b0 » fxn
2010-06-14 edit pass: the names of Rails components have a space, ie, "Active Re…
39 # default behavior for Active Record when used in conjunction with
40 # Action Pack's request handling cycle.
fe575dd4 » nicksieger
2008-08-07 Nearing the finish line. Initial fixed-size connection pool implement…
41 # 2. Manually check out a connection from the pool with
42 # ActiveRecord::Base.connection_pool.checkout. You are responsible for
43 # returning this connection to the pool when finished by calling
44 # ActiveRecord::Base.connection_pool.checkin(connection).
45 # 3. Use ActiveRecord::Base.connection_pool.with_connection(&block), which
46 # obtains a connection, yields it as the sole argument to the block,
47 # and returns it to the pool after the block completes.
8e5e02bd » nicksieger
2008-08-22 Collapse connection pool class hierarchy; YAGNI.
48 #
a2932784 » lifo
2008-10-05 Merge docrails
49 # Connections in the pool are actually AbstractAdapter objects (or objects
50 # compatible with AbstractAdapter's interface).
51 #
52 # == Options
53 #
8e5e02bd » nicksieger
2008-08-22 Collapse connection pool class hierarchy; YAGNI.
54 # There are two connection-pooling-related options that you can add to
55 # your database connection configuration:
56 #
57 # * +pool+: number indicating size of connection pool (default 5)
58 # * +wait_timeout+: number of seconds to block and wait for a connection
59 # before giving up and raising a timeout error (default 5 seconds).
817a07b4 » nicksieger
2008-08-07 More doco and class/method renames. Now have a strategy for integrati…
60 class ConnectionPool
acccb72c » tenderlove
2011-02-04 column cache now lives on the connection pool
61 attr_accessor :automatic_reconnect
cccb9988 » josevalim
2010-01-11 Expose connections available in the connection pool.
62 attr_reader :spec, :connections
82fcd9d8 » nicksieger
2008-08-06 Clean up the code, get rid of reserve/release, add some more docs
63
a2932784 » lifo
2008-10-05 Merge docrails
64 # Creates a new ConnectionPool object. +spec+ is a ConnectionSpecification
65 # object which describes database connection information (e.g. adapter,
66 # host name, username, password, etc), as well as the maximum size for
67 # this ConnectionPool.
68 #
69 # The default ConnectionPool maximum size is 5.
6edaa267 » nicksieger
2008-04-19 Initial conversion to connection pool
70 def initialize(spec)
71 @spec = spec
dd77733f » jeremy
2008-11-08 Timeout the connection pool monitor on ruby 1.8 only
72
029952ed » nicksieger
2008-08-04 Extract a base class for connection pools, start to flesh out reserve…
73 # The cache of reserved connections mapped to threads
74 @reserved_connections = {}
dd77733f » jeremy
2008-11-08 Timeout the connection pool monitor on ruby 1.8 only
75
cab76ce6 » nicksieger
2008-04-19 Add synchronization to connection pool also
76 # The mutex used to synchronize pool access
77 @connection_mutex = Monitor.new
8e5e02bd » nicksieger
2008-08-22 Collapse connection pool class hierarchy; YAGNI.
78 @queue = @connection_mutex.new_cond
2a04110f » Hemant Kumar
2010-09-30 fix ruby 1.9 deadlock problem, fixes #5736 add connection pool tests
79 @timeout = spec.config[:wait_timeout] || 5
dd77733f » jeremy
2008-11-08 Timeout the connection pool monitor on ruby 1.8 only
80
8e5e02bd » nicksieger
2008-08-22 Collapse connection pool class hierarchy; YAGNI.
81 # default max pool size to 5
82 @size = (spec.config[:pool] && spec.config[:pool].to_i) || 5
dd77733f » jeremy
2008-11-08 Timeout the connection pool monitor on ruby 1.8 only
83
7db90aa7 » jonleighton
2011-08-08 Make it the responsibility of the connection to hold onto an ARel vis…
84 @connections = []
85 @checked_out = []
acccb72c » tenderlove
2011-02-04 column cache now lives on the connection pool
86 @automatic_reconnect = true
7db90aa7 » jonleighton
2011-08-08 Make it the responsibility of the connection to hold onto an ARel vis…
87 @visitor = nil
c94651f8 » tenderlove
2011-02-04 almost fisted
88 end
89
82fcd9d8 » nicksieger
2008-08-06 Clean up the code, get rid of reserve/release, add some more docs
90 # Retrieve the connection associated with the current thread, or call
91 # #checkout to obtain one if necessary.
92 #
93 # #connection can be called any number of times; the connection is
94 # held in a hash keyed by the thread id.
95 def connection
072cd603 » tenderlove
2010-08-18 refactor if / else to ||=
96 @reserved_connections[current_connection_id] ||= checkout
6edaa267 » nicksieger
2008-04-19 Initial conversion to connection pool
97 end
98
4211866b » tenderlove
2011-03-28 adding active_connection? to the connection pool
99 # Check to see if there is an active connection in this connection
100 # pool.
101 def active_connection?
102 @reserved_connections.key? current_connection_id
103 end
104
82fcd9d8 » nicksieger
2008-08-06 Clean up the code, get rid of reserve/release, add some more docs
105 # Signal that the thread is finished with the current connection.
817a07b4 » nicksieger
2008-08-07 More doco and class/method renames. Now have a strategy for integrati…
106 # #release_connection releases the connection-thread association
82fcd9d8 » nicksieger
2008-08-06 Clean up the code, get rid of reserve/release, add some more docs
107 # and returns the connection to the pool.
33445209 » tenderlove
2010-08-06 reduce the number of times current_connection_id is called in with_co…
108 def release_connection(with_id = current_connection_id)
109 conn = @reserved_connections.delete(with_id)
82fcd9d8 » nicksieger
2008-08-06 Clean up the code, get rid of reserve/release, add some more docs
110 checkin conn if conn
111 end
112
0034b782 » smartinez87
2011-05-23 Remove extra white spaces on ActiveRecord docs.
113 # If a connection already exists yield it to the block. If no connection
b451de0d » spastorino
2010-08-14 Deletes trailing whitespaces (over text files only find * -type f -ex…
114 # exists checkout a connection, yield it to the block, and checkin the
5501b99a » coderrr
2009-05-01 Ensure ActiveRecord::Base.connection_pool.with_connection creates a n…
115 # connection when finished.
82fcd9d8 » nicksieger
2008-08-06 Clean up the code, get rid of reserve/release, add some more docs
116 def with_connection
33445209 » tenderlove
2010-08-06 reduce the number of times current_connection_id is called in with_co…
117 connection_id = current_connection_id
118 fresh_connection = true unless @reserved_connections[connection_id]
5501b99a » coderrr
2009-05-01 Ensure ActiveRecord::Base.connection_pool.with_connection creates a n…
119 yield connection
82fcd9d8 » nicksieger
2008-08-06 Clean up the code, get rid of reserve/release, add some more docs
120 ensure
33445209 » tenderlove
2010-08-06 reduce the number of times current_connection_id is called in with_co…
121 release_connection(connection_id) if fresh_connection
6edaa267 » nicksieger
2008-04-19 Initial conversion to connection pool
122 end
123
029952ed » nicksieger
2008-08-04 Extract a base class for connection pools, start to flesh out reserve…
124 # Returns true if a connection has already been opened.
125 def connected?
8e5e02bd » nicksieger
2008-08-22 Collapse connection pool class hierarchy; YAGNI.
126 !@connections.empty?
6edaa267 » nicksieger
2008-04-19 Initial conversion to connection pool
127 end
128
a2932784 » lifo
2008-10-05 Merge docrails
129 # Disconnects all connections in the pool, and clears the pool.
029952ed » nicksieger
2008-08-04 Extract a base class for connection pools, start to flesh out reserve…
130 def disconnect!
131 @reserved_connections.each do |name,conn|
82fcd9d8 » nicksieger
2008-08-06 Clean up the code, get rid of reserve/release, add some more docs
132 checkin conn
029952ed » nicksieger
2008-08-04 Extract a base class for connection pools, start to flesh out reserve…
133 end
8e5e02bd » nicksieger
2008-08-22 Collapse connection pool class hierarchy; YAGNI.
134 @reserved_connections = {}
135 @connections.each do |conn|
6edaa267 » nicksieger
2008-04-19 Initial conversion to connection pool
136 conn.disconnect!
137 end
8e5e02bd » nicksieger
2008-08-22 Collapse connection pool class hierarchy; YAGNI.
138 @connections = []
6edaa267 » nicksieger
2008-04-19 Initial conversion to connection pool
139 end
140
8f1b141b » smartinez87
2011-04-22 Fixed punctuation errors.
141 # Clears the cache which maps classes.
6edaa267 » nicksieger
2008-04-19 Initial conversion to connection pool
142 def clear_reloadable_connections!
029952ed » nicksieger
2008-08-04 Extract a base class for connection pools, start to flesh out reserve…
143 @reserved_connections.each do |name, conn|
82fcd9d8 » nicksieger
2008-08-06 Clean up the code, get rid of reserve/release, add some more docs
144 checkin conn
029952ed » nicksieger
2008-08-04 Extract a base class for connection pools, start to flesh out reserve…
145 end
146 @reserved_connections = {}
8e5e02bd » nicksieger
2008-08-22 Collapse connection pool class hierarchy; YAGNI.
147 @connections.each do |conn|
148 conn.disconnect! if conn.requires_reloading?
6edaa267 » nicksieger
2008-04-19 Initial conversion to connection pool
149 end
62c4e4d3 » ebeigarts
2010-06-22 Fix connection reloading in development mode. [#4929 state:resolved]
150 @connections.delete_if do |conn|
151 conn.requires_reloading?
152 end
6edaa267 » nicksieger
2008-04-19 Initial conversion to connection pool
153 end
154
817a07b4 » nicksieger
2008-08-07 More doco and class/method renames. Now have a strategy for integrati…
155 # Verify active connections and remove and disconnect connections
156 # associated with stale threads.
6edaa267 » nicksieger
2008-04-19 Initial conversion to connection pool
157 def verify_active_connections! #:nodoc:
d07a6b1a » nicksieger
2008-08-22 Make clear_active_connections! also return stale connections back to …
158 clear_stale_cached_connections!
8e5e02bd » nicksieger
2008-08-22 Collapse connection pool class hierarchy; YAGNI.
159 @connections.each do |connection|
7ba28726 » nicksieger
2008-09-04 Deprecate verification_timeout and verify before reset
160 connection.verify!
6edaa267 » nicksieger
2008-04-19 Initial conversion to connection pool
161 end
162 end
163
bd2f5c06 » tenderlove
2011-11-19 pushing caching and visitors down to the connection
164 def columns
165 with_connection do |c|
166 c.schema_cache.columns
167 end
168 end
169 deprecate :columns
170
171 def columns_hash
172 with_connection do |c|
173 c.schema_cache.columns_hash
174 end
175 end
176 deprecate :columns_hash
177
178 def primary_keys
179 with_connection do |c|
180 c.schema_cache.primary_keys
181 end
182 end
183 deprecate :primary_keys
184
185 def clear_cache!
186 with_connection do |c|
187 c.schema_cache.clear!
188 end
189 end
190 deprecate :clear_cache!
191
d07a6b1a » nicksieger
2008-08-22 Make clear_active_connections! also return stale connections back to …
192 # Return any checked-out connections back to the pool by threads that
193 # are no longer alive.
194 def clear_stale_cached_connections!
42be67e8 » tenderlove
2010-07-16 unfactoring clear_stale_cached_connections!
195 keys = @reserved_connections.keys - Thread.list.find_all { |t|
196 t.alive?
197 }.map { |thread| thread.object_id }
198 keys.each do |key|
199 checkin @reserved_connections[key]
200 @reserved_connections.delete(key)
d07a6b1a » nicksieger
2008-08-22 Make clear_active_connections! also return stale connections back to …
201 end
202 end
203
a2932784 » lifo
2008-10-05 Merge docrails
204 # Check-out a database connection from the pool, indicating that you want
205 # to use it. You should call #checkin when you no longer need this.
206 #
207 # This is done by either returning an existing connection, or by creating
208 # a new connection. If the maximum number of connections for this pool has
209 # already been reached, but the pool is empty (i.e. they're all being used),
210 # then this method will wait until a thread has checked in a connection.
211 # The wait time is bounded however: if no connection can be checked out
212 # within the timeout specified for this pool, then a ConnectionTimeoutError
213 # exception will be raised.
214 #
215 # Returns: an AbstractAdapter object.
216 #
217 # Raises:
218 # - ConnectionTimeoutError: no connection can be obtained from the pool
219 # within the timeout period.
82fcd9d8 » nicksieger
2008-08-06 Clean up the code, get rid of reserve/release, add some more docs
220 def checkout
8e5e02bd » nicksieger
2008-08-22 Collapse connection pool class hierarchy; YAGNI.
221 # Checkout an available connection
222 @connection_mutex.synchronize do
21eb18a7 » alk
2008-10-02 Fix race in ConnectionPool#checkout
223 loop do
224 conn = if @checked_out.size < @connections.size
225 checkout_existing_connection
226 elsif @connections.size < @size
227 checkout_new_connection
228 end
229 return conn if conn
2a04110f » Hemant Kumar
2010-09-30 fix ruby 1.9 deadlock problem, fixes #5736 add connection pool tests
230
231 @queue.wait(@timeout)
232
233 if(@checked_out.size < @connections.size)
21eb18a7 » alk
2008-10-02 Fix race in ConnectionPool#checkout
234 next
235 else
83436114 » alk
2008-10-02 made ConnectionPool#checkout more robust by trying to loot dead threa…
236 clear_stale_cached_connections!
237 if @size == @checked_out.size
05f29ca2 » smartinez87
2011-05-23 Remove extra white-space on some exception messages.
238 raise ConnectionTimeoutError, "could not obtain a database connection#{" within #{@timeout} seconds" if @timeout}. The max pool size is currently #{@size}; consider increasing it."
83436114 » alk
2008-10-02 made ConnectionPool#checkout more robust by trying to loot dead threa…
239 end
21eb18a7 » alk
2008-10-02 Fix race in ConnectionPool#checkout
240 end
2a04110f » Hemant Kumar
2010-09-30 fix ruby 1.9 deadlock problem, fixes #5736 add connection pool tests
241
8e5e02bd » nicksieger
2008-08-22 Collapse connection pool class hierarchy; YAGNI.
242 end
243 end
6edaa267 » nicksieger
2008-04-19 Initial conversion to connection pool
244 end
245
a2932784 » lifo
2008-10-05 Merge docrails
246 # Check-in a database connection back into the pool, indicating that you
247 # no longer need this connection.
248 #
249 # +conn+: an AbstractAdapter object, which was obtained by earlier by
250 # calling +checkout+ on this pool.
8e5e02bd » nicksieger
2008-08-22 Collapse connection pool class hierarchy; YAGNI.
251 def checkin(conn)
252 @connection_mutex.synchronize do
57bc25c5 » jfirebaugh
2011-01-09 Use run_callbacks; the generated _run_<name>_callbacks method is not …
253 conn.run_callbacks :checkin do
471a3942 » nicksieger
2009-10-16 Modify connection pool callbacks to be compatible w/ new style
254 @checked_out.delete conn
255 @queue.signal
256 end
8e5e02bd » nicksieger
2008-08-22 Collapse connection pool class hierarchy; YAGNI.
257 end
6edaa267 » nicksieger
2008-04-19 Initial conversion to connection pool
258 end
82fcd9d8 » nicksieger
2008-08-06 Clean up the code, get rid of reserve/release, add some more docs
259
113cc4e1 » nicksieger
2008-08-23 Remove some synchronization that's probably overkill, assuming one do…
260 synchronize :clear_reloadable_connections!, :verify_active_connections!,
82fcd9d8 » nicksieger
2008-08-06 Clean up the code, get rid of reserve/release, add some more docs
261 :connected?, :disconnect!, :with => :@connection_mutex
262
263 private
7db90aa7 » jonleighton
2011-08-08 Make it the responsibility of the connection to hold onto an ARel vis…
264
fe575dd4 » nicksieger
2008-08-07 Nearing the finish line. Initial fixed-size connection pool implement…
265 def new_connection
bd2f5c06 » tenderlove
2011-11-19 pushing caching and visitors down to the connection
266 ActiveRecord::Base.send(spec.adapter_method, spec.config)
fe575dd4 » nicksieger
2008-08-07 Nearing the finish line. Initial fixed-size connection pool implement…
267 end
268
d07a6b1a » nicksieger
2008-08-22 Make clear_active_connections! also return stale connections back to …
269 def current_connection_id #:nodoc:
f41b58d3 » mjtko
2011-06-13 use thread locals and an instance variable within QueryCache#BodyProx…
270 ActiveRecord::Base.connection_id ||= Thread.current.object_id
82fcd9d8 » nicksieger
2008-08-06 Clean up the code, get rid of reserve/release, add some more docs
271 end
6edaa267 » nicksieger
2008-04-19 Initial conversion to connection pool
272
fe575dd4 » nicksieger
2008-08-07 Nearing the finish line. Initial fixed-size connection pool implement…
273 def checkout_new_connection
acccb72c » tenderlove
2011-02-04 column cache now lives on the connection pool
274 raise ConnectionNotEstablished unless @automatic_reconnect
275
fe575dd4 » nicksieger
2008-08-07 Nearing the finish line. Initial fixed-size connection pool implement…
276 c = new_connection
277 @connections << c
a96b7d4c » nicksieger
2008-08-22 Add connection reset and verification upon each connection checkout
278 checkout_and_verify(c)
fe575dd4 » nicksieger
2008-08-07 Nearing the finish line. Initial fixed-size connection pool implement…
279 end
280
281 def checkout_existing_connection
d7d2d73d » nicksieger
2008-08-08 Fix typo: was using brackets instead of parens. Must need more sleep.
282 c = (@connections - @checked_out).first
a96b7d4c » nicksieger
2008-08-22 Add connection reset and verification upon each connection checkout
283 checkout_and_verify(c)
284 end
285
286 def checkout_and_verify(c)
471a3942 » nicksieger
2009-10-16 Modify connection pool callbacks to be compatible w/ new style
287 c.run_callbacks :checkout do
288 c.verify!
289 @checked_out << c
290 end
fe575dd4 » nicksieger
2008-08-07 Nearing the finish line. Initial fixed-size connection pool implement…
291 c
292 end
293 end
294
a2932784 » lifo
2008-10-05 Merge docrails
295 # ConnectionHandler is a collection of ConnectionPool objects. It is used
f17159b0 » fxn
2010-06-14 edit pass: the names of Rails components have a space, ie, "Active Re…
296 # for keeping separate connection pools for Active Record models that connect
a2932784 » lifo
2008-10-05 Merge docrails
297 # to different databases.
298 #
299 # For example, suppose that you have 5 models, with the following hierarchy:
300 #
301 # |
302 # +-- Book
303 # | |
304 # | +-- ScaryBook
305 # | +-- GoodBook
306 # +-- Author
307 # +-- BankAccount
308 #
309 # Suppose that Book is to connect to a separate database (i.e. one other
310 # than the default database). Then Book, ScaryBook and GoodBook will all use
311 # the same connection pool. Likewise, Author and BankAccount will use the
312 # same connection pool. However, the connection pool used by Author/BankAccount
313 # is not the same as the one used by Book/ScaryBook/GoodBook.
314 #
315 # Normally there is only a single ConnectionHandler instance, accessible via
f17159b0 » fxn
2010-06-14 edit pass: the names of Rails components have a space, ie, "Active Re…
316 # ActiveRecord::Base.connection_handler. Active Record models use this to
a2932784 » lifo
2008-10-05 Merge docrails
317 # determine that connection pool that they should use.
ca6d7175 » nicksieger
2008-08-22 Deprecate allow_concurrency and make it have no effect
318 class ConnectionHandler
b952470c » tenderlove
2010-07-16 use an attr_reader for performance
319 attr_reader :connection_pools
320
72d959d9 » nicksieger
2008-06-07 Split connection handler into single- and multiple-thread versions.
321 def initialize(pools = {})
322 @connection_pools = pools
ffb218c7 » tenderlove
2011-11-28 pools are 1:1 with spec now rather than 1:1 with class
323 @class_to_pool = {}
72d959d9 » nicksieger
2008-06-07 Split connection handler into single- and multiple-thread versions.
324 end
ff97e9d0 » nicksieger
2008-06-07 Connection handling methods extracted out into separate ConnectionHan…
325
326 def establish_connection(name, spec)
ffb218c7 » tenderlove
2011-11-28 pools are 1:1 with spec now rather than 1:1 with class
327 @connection_pools[spec] ||= ConnectionAdapters::ConnectionPool.new(spec)
328 @class_to_pool[name] = @connection_pools[spec]
ff97e9d0 » nicksieger
2008-06-07 Connection handling methods extracted out into separate ConnectionHan…
329 end
330
25f94971 » tenderlove
2011-03-28 adding active_connections? to the connection pool for finding open co…
331 # Returns true if there are any active connections among the connection
332 # pools that the ConnectionHandler is managing.
333 def active_connections?
334 connection_pools.values.any? { |pool| pool.active_connection? }
335 end
336
d07a6b1a » nicksieger
2008-08-22 Make clear_active_connections! also return stale connections back to …
337 # Returns any connections in use by the current thread back to the pool,
338 # and also returns connections to the pool cached by threads that are no
339 # longer alive.
ff97e9d0 » nicksieger
2008-06-07 Connection handling methods extracted out into separate ConnectionHan…
340 def clear_active_connections!
529c2716 » nicksieger
2008-11-08 Simplify dispatcher callbacks to eliminate unnecessary stale thread p…
341 @connection_pools.each_value {|pool| pool.release_connection }
ff97e9d0 » nicksieger
2008-06-07 Connection handling methods extracted out into separate ConnectionHan…
342 end
343
8f1b141b » smartinez87
2011-04-22 Fixed punctuation errors.
344 # Clears the cache which maps classes.
ff97e9d0 » nicksieger
2008-06-07 Connection handling methods extracted out into separate ConnectionHan…
345 def clear_reloadable_connections!
346 @connection_pools.each_value {|pool| pool.clear_reloadable_connections! }
347 end
348
349 def clear_all_connections!
72d959d9 » nicksieger
2008-06-07 Split connection handler into single- and multiple-thread versions.
350 @connection_pools.each_value {|pool| pool.disconnect! }
ff97e9d0 » nicksieger
2008-06-07 Connection handling methods extracted out into separate ConnectionHan…
351 end
352
353 # Verify active connections.
354 def verify_active_connections! #:nodoc:
817a07b4 » nicksieger
2008-08-07 More doco and class/method renames. Now have a strategy for integrati…
355 @connection_pools.each_value {|pool| pool.verify_active_connections! }
ff97e9d0 » nicksieger
2008-06-07 Connection handling methods extracted out into separate ConnectionHan…
356 end
357
358 # Locate the connection of the nearest super class. This can be an
359 # active or defined connection: if it is the latter, it will be
360 # opened and set as the active connection for the class it was defined
361 # for (not necessarily the current class).
362 def retrieve_connection(klass) #:nodoc:
363 pool = retrieve_connection_pool(klass)
364 (pool && pool.connection) or raise ConnectionNotEstablished
365 end
366
82fcd9d8 » nicksieger
2008-08-06 Clean up the code, get rid of reserve/release, add some more docs
367 # Returns true if a connection that's accessible to this class has
368 # already been opened.
ff97e9d0 » nicksieger
2008-06-07 Connection handling methods extracted out into separate ConnectionHan…
369 def connected?(klass)
0832bc63 » lifo
2008-11-06 Make sure ActiveRecord::Base.connected? doesn't raise an exception fo…
370 conn = retrieve_connection_pool(klass)
a4458f5d » tenderlove
2010-08-19 removing useless ternary
371 conn && conn.connected?
ff97e9d0 » nicksieger
2008-06-07 Connection handling methods extracted out into separate ConnectionHan…
372 end
373
374 # Remove the connection for this class. This will close the active
375 # connection and the defined connection (if they exist). The result
376 # can be used as an argument for establish_connection, for easily
377 # re-establishing the connection.
378 def remove_connection(klass)
ffb218c7 » tenderlove
2011-11-28 pools are 1:1 with spec now rather than 1:1 with class
379 pool = @class_to_pool.delete(klass.name)
04ef434b » tenderlove
2010-07-16 only test for existence of +pool+ once
380 return nil unless pool
381
ffb218c7 » tenderlove
2011-11-28 pools are 1:1 with spec now rather than 1:1 with class
382 @connection_pools.delete pool.spec
acccb72c » tenderlove
2011-02-04 column cache now lives on the connection pool
383 pool.automatic_reconnect = false
04ef434b » tenderlove
2010-07-16 only test for existence of +pool+ once
384 pool.disconnect!
385 pool.spec.config
ff97e9d0 » nicksieger
2008-06-07 Connection handling methods extracted out into separate ConnectionHan…
386 end
387
fe575dd4 » nicksieger
2008-08-07 Nearing the finish line. Initial fixed-size connection pool implement…
388 def retrieve_connection_pool(klass)
ffb218c7 » tenderlove
2011-11-28 pools are 1:1 with spec now rather than 1:1 with class
389 pool = @class_to_pool[klass.name]
30075450 » nicksieger
2008-08-26 Minor tweak to retrieve_connection_pool -- recurse instead of loop
390 return pool if pool
391 return nil if ActiveRecord::Base == klass
392 retrieve_connection_pool klass.superclass
fe575dd4 » nicksieger
2008-08-07 Nearing the finish line. Initial fixed-size connection pool implement…
393 end
72d959d9 » nicksieger
2008-06-07 Split connection handler into single- and multiple-thread versions.
394 end
1b22071b » josh
2009-02-24 Ensure ActiveRecord session store's connections are checked in after …
395
396 class ConnectionManagement
e5246092 » tenderlove
2011-03-29 proxy body responses so we close database connections after body is f…
397 class Proxy # :nodoc:
398 attr_reader :body, :testing
399
400 def initialize(body, testing = false)
401 @body = body
402 @testing = testing
403 end
404
50444204 » stevehodgkiss
2011-06-18 Fix inconsistencies by being polite to the wrapped body. Needed for R…
405 def method_missing(method_sym, *arguments, &block)
406 @body.send(method_sym, *arguments, &block)
407 end
408
409 def respond_to?(method_sym, include_private = false)
410 super || @body.respond_to?(method_sym)
411 end
412
e5246092 » tenderlove
2011-03-29 proxy body responses so we close database connections after body is f…
413 def each(&block)
414 body.each(&block)
415 end
416
417 def close
418 body.close if body.respond_to?(:close)
419
420 # Don't return connection (and perform implicit rollback) if
421 # this request is a part of integration test
422 ActiveRecord::Base.clear_active_connections! unless testing
423 end
424 end
425
1b22071b » josh
2009-02-24 Ensure ActiveRecord session store's connections are checked in after …
426 def initialize(app)
427 @app = app
428 end
429
430 def call(env)
c7b7c6ad » tenderlove
2011-03-29 make sure that active connections are not cleared during test when an…
431 testing = env.key?('rack.test')
432
e5246092 » tenderlove
2011-03-29 proxy body responses so we close database connections after body is f…
433 status, headers, body = @app.call(env)
434
c7b7c6ad » tenderlove
2011-03-29 make sure that active connections are not cleared during test when an…
435 [status, headers, Proxy.new(body, testing)]
3b2a0326 » tenderlove
2011-03-29 clearing active connections in the ConnectionManagement middleware if…
436 rescue
c7b7c6ad » tenderlove
2011-03-29 make sure that active connections are not cleared during test when an…
437 ActiveRecord::Base.clear_active_connections! unless testing
3b2a0326 » tenderlove
2011-03-29 clearing active connections in the ConnectionManagement middleware if…
438 raise
1b22071b » josh
2009-02-24 Ensure ActiveRecord session store's connections are checked in after …
439 end
440 end
6edaa267 » nicksieger
2008-04-19 Initial conversion to connection pool
441 end
21eb18a7 » alk
2008-10-02 Fix race in ConnectionPool#checkout
442 end
Something went wrong with that request. Please try again.