Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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