Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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