Skip to content

HTTPS clone URL

Subversion checkout URL

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