Skip to content
Newer
Older
100644 366 lines (326 sloc) 13 KB
ff97e9d @nicksieger Connection handling methods extracted out into separate ConnectionHan…
nicksieger authored Jun 7, 2008
1 require 'monitor'
50cd4bd @nicksieger Introduce synchronization around connection pool access
nicksieger authored Apr 19, 2008
2 require 'set'
e8550ee @jeremy Cherry-pick core extensions
jeremy authored May 13, 2009
3 require 'active_support/core_ext/module/synchronization'
50cd4bd @nicksieger Introduce synchronization around connection pool access
nicksieger authored Apr 19, 2008
4
6edaa26 @nicksieger Initial conversion to connection pool
nicksieger authored Apr 19, 2008
5 module ActiveRecord
fe575dd @nicksieger Nearing the finish line. Initial fixed-size connection pool implement…
nicksieger authored Aug 7, 2008
6 # Raised when a connection could not be obtained within the connection
7 # acquisition timeout period.
8 class ConnectionTimeoutError < ConnectionNotEstablished
9 end
10
6edaa26 @nicksieger Initial conversion to connection pool
nicksieger authored Apr 19, 2008
11 module ConnectionAdapters
f17159b @fxn edit pass: the names of Rails components have a space, ie, "Active Re…
fxn authored Jun 14, 2010
12 # Connection pool base class for managing Active Record database
fe575dd @nicksieger Nearing the finish line. Initial fixed-size connection pool implement…
nicksieger authored Aug 7, 2008
13 # connections.
14 #
a293278 @lifo Merge docrails
lifo authored Oct 5, 2008
15 # == Introduction
16 #
17 # A connection pool synchronizes thread access to a limited number of
18 # database connections. The basic idea is that each thread checks out a
19 # database connection from the pool, uses that connection, and checks the
20 # connection back in. ConnectionPool is completely thread-safe, and will
21 # ensure that a connection cannot be used by two threads at the same time,
22 # as long as ConnectionPool's contract is correctly followed. It will also
23 # handle cases in which there are more threads than connections: if all
24 # connections have been checked out, and a thread tries to checkout a
25 # connection anyway, then ConnectionPool will wait until some other thread
26 # has checked in a connection.
27 #
28 # == Obtaining (checking out) a connection
29 #
fe575dd @nicksieger Nearing the finish line. Initial fixed-size connection pool implement…
nicksieger authored Aug 7, 2008
30 # Connections can be obtained and used from a connection pool in several
31 # ways:
32 #
f17159b @fxn edit pass: the names of Rails components have a space, ie, "Active Re…
fxn authored Jun 14, 2010
33 # 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 Aug 7, 2008
34 # earlier (pre-connection-pooling). Eventually, when you're done with
35 # the connection(s) and wish it to be returned to the pool, you call
36 # 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 Jun 14, 2010
37 # default behavior for Active Record when used in conjunction with
38 # Action Pack's request handling cycle.
fe575dd @nicksieger Nearing the finish line. Initial fixed-size connection pool implement…
nicksieger authored Aug 7, 2008
39 # 2. Manually check out a connection from the pool with
40 # ActiveRecord::Base.connection_pool.checkout. You are responsible for
41 # returning this connection to the pool when finished by calling
42 # ActiveRecord::Base.connection_pool.checkin(connection).
43 # 3. Use ActiveRecord::Base.connection_pool.with_connection(&block), which
44 # obtains a connection, yields it as the sole argument to the block,
45 # and returns it to the pool after the block completes.
8e5e02b @nicksieger Collapse connection pool class hierarchy; YAGNI.
nicksieger authored Aug 22, 2008
46 #
a293278 @lifo Merge docrails
lifo authored Oct 5, 2008
47 # Connections in the pool are actually AbstractAdapter objects (or objects
48 # compatible with AbstractAdapter's interface).
49 #
50 # == Options
51 #
8e5e02b @nicksieger Collapse connection pool class hierarchy; YAGNI.
nicksieger authored Aug 22, 2008
52 # There are two connection-pooling-related options that you can add to
53 # your database connection configuration:
54 #
55 # * +pool+: number indicating size of connection pool (default 5)
56 # * +wait_timeout+: number of seconds to block and wait for a connection
57 # 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 Aug 8, 2008
58 class ConnectionPool
cccb998 @josevalim Expose connections available in the connection pool.
josevalim authored Jan 11, 2010
59 attr_reader :spec, :connections
82fcd9d @nicksieger Clean up the code, get rid of reserve/release, add some more docs
nicksieger authored Aug 6, 2008
60
a293278 @lifo Merge docrails
lifo authored Oct 5, 2008
61 # Creates a new ConnectionPool object. +spec+ is a ConnectionSpecification
62 # object which describes database connection information (e.g. adapter,
63 # host name, username, password, etc), as well as the maximum size for
64 # this ConnectionPool.
65 #
66 # The default ConnectionPool maximum size is 5.
6edaa26 @nicksieger Initial conversion to connection pool
nicksieger authored Apr 19, 2008
67 def initialize(spec)
68 @spec = spec
dd77733 @jeremy Timeout the connection pool monitor on ruby 1.8 only
jeremy authored Nov 8, 2008
69
029952e @nicksieger Extract a base class for connection pools, start to flesh out reserve…
nicksieger authored Aug 4, 2008
70 # The cache of reserved connections mapped to threads
71 @reserved_connections = {}
dd77733 @jeremy Timeout the connection pool monitor on ruby 1.8 only
jeremy authored Nov 8, 2008
72
cab76ce @nicksieger Add synchronization to connection pool also
nicksieger authored Apr 19, 2008
73 # The mutex used to synchronize pool access
74 @connection_mutex = Monitor.new
8e5e02b @nicksieger Collapse connection pool class hierarchy; YAGNI.
nicksieger authored Aug 22, 2008
75 @queue = @connection_mutex.new_cond
dd77733 @jeremy Timeout the connection pool monitor on ruby 1.8 only
jeremy authored Nov 8, 2008
76
77 # default 5 second timeout unless on ruby 1.9
78 @timeout =
79 if RUBY_VERSION < '1.9'
80 spec.config[:wait_timeout] || 5
81 end
82
8e5e02b @nicksieger Collapse connection pool class hierarchy; YAGNI.
nicksieger authored Aug 22, 2008
83 # default max pool size to 5
84 @size = (spec.config[:pool] && spec.config[:pool].to_i) || 5
dd77733 @jeremy Timeout the connection pool monitor on ruby 1.8 only
jeremy authored Nov 8, 2008
85
8e5e02b @nicksieger Collapse connection pool class hierarchy; YAGNI.
nicksieger authored Aug 22, 2008
86 @connections = []
87 @checked_out = []
6edaa26 @nicksieger Initial conversion to connection pool
nicksieger authored Apr 19, 2008
88 end
89
82fcd9d @nicksieger Clean up the code, get rid of reserve/release, add some more docs
nicksieger authored Aug 7, 2008
90 # Retrieve the connection associated with the current thread, or call
91 # #checkout to obtain one if necessary.
92 #
93 # #connection can be called any number of times; the connection is
94 # held in a hash keyed by the thread id.
95 def connection
072cd60 @tenderlove refactor if / else to ||=
tenderlove authored Aug 18, 2010
96 @reserved_connections[current_connection_id] ||= checkout
6edaa26 @nicksieger Initial conversion to connection pool
nicksieger authored Apr 19, 2008
97 end
98
82fcd9d @nicksieger Clean up the code, get rid of reserve/release, add some more docs
nicksieger authored Aug 7, 2008
99 # 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 Aug 8, 2008
100 # #release_connection releases the connection-thread association
82fcd9d @nicksieger Clean up the code, get rid of reserve/release, add some more docs
nicksieger authored Aug 7, 2008
101 # and returns the connection to the pool.
3344520 @tenderlove reduce the number of times current_connection_id is called in with_co…
tenderlove authored Aug 6, 2010
102 def release_connection(with_id = current_connection_id)
103 conn = @reserved_connections.delete(with_id)
82fcd9d @nicksieger Clean up the code, get rid of reserve/release, add some more docs
nicksieger authored Aug 7, 2008
104 checkin conn if conn
105 end
106
5501b99 @account-settings Ensure ActiveRecord::Base.connection_pool.with_connection creates a n…
account-settings authored May 1, 2009
107 # 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 Aug 14, 2010
108 # 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 May 1, 2009
109 # connection when finished.
82fcd9d @nicksieger Clean up the code, get rid of reserve/release, add some more docs
nicksieger authored Aug 7, 2008
110 def with_connection
3344520 @tenderlove reduce the number of times current_connection_id is called in with_co…
tenderlove authored Aug 6, 2010
111 connection_id = current_connection_id
112 fresh_connection = true unless @reserved_connections[connection_id]
5501b99 @account-settings Ensure ActiveRecord::Base.connection_pool.with_connection creates a n…
account-settings authored May 1, 2009
113 yield connection
82fcd9d @nicksieger Clean up the code, get rid of reserve/release, add some more docs
nicksieger authored Aug 7, 2008
114 ensure
3344520 @tenderlove reduce the number of times current_connection_id is called in with_co…
tenderlove authored Aug 6, 2010
115 release_connection(connection_id) if fresh_connection
6edaa26 @nicksieger Initial conversion to connection pool
nicksieger authored Apr 19, 2008
116 end
117
029952e @nicksieger Extract a base class for connection pools, start to flesh out reserve…
nicksieger authored Aug 5, 2008
118 # Returns true if a connection has already been opened.
119 def connected?
8e5e02b @nicksieger Collapse connection pool class hierarchy; YAGNI.
nicksieger authored Aug 22, 2008
120 !@connections.empty?
6edaa26 @nicksieger Initial conversion to connection pool
nicksieger authored Apr 19, 2008
121 end
122
a293278 @lifo Merge docrails
lifo authored Oct 5, 2008
123 # 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 Aug 5, 2008
124 def disconnect!
125 @reserved_connections.each do |name,conn|
82fcd9d @nicksieger Clean up the code, get rid of reserve/release, add some more docs
nicksieger authored Aug 7, 2008
126 checkin conn
029952e @nicksieger Extract a base class for connection pools, start to flesh out reserve…
nicksieger authored Aug 5, 2008
127 end
8e5e02b @nicksieger Collapse connection pool class hierarchy; YAGNI.
nicksieger authored Aug 22, 2008
128 @reserved_connections = {}
129 @connections.each do |conn|
6edaa26 @nicksieger Initial conversion to connection pool
nicksieger authored Apr 19, 2008
130 conn.disconnect!
131 end
8e5e02b @nicksieger Collapse connection pool class hierarchy; YAGNI.
nicksieger authored Aug 22, 2008
132 @connections = []
6edaa26 @nicksieger Initial conversion to connection pool
nicksieger authored Apr 19, 2008
133 end
134
50cd4bd @nicksieger Introduce synchronization around connection pool access
nicksieger authored Apr 19, 2008
135 # Clears the cache which maps classes
6edaa26 @nicksieger Initial conversion to connection pool
nicksieger authored Apr 19, 2008
136 def clear_reloadable_connections!
029952e @nicksieger Extract a base class for connection pools, start to flesh out reserve…
nicksieger authored Aug 5, 2008
137 @reserved_connections.each do |name, conn|
82fcd9d @nicksieger Clean up the code, get rid of reserve/release, add some more docs
nicksieger authored Aug 7, 2008
138 checkin conn
029952e @nicksieger Extract a base class for connection pools, start to flesh out reserve…
nicksieger authored Aug 5, 2008
139 end
140 @reserved_connections = {}
8e5e02b @nicksieger Collapse connection pool class hierarchy; YAGNI.
nicksieger authored Aug 22, 2008
141 @connections.each do |conn|
142 conn.disconnect! if conn.requires_reloading?
6edaa26 @nicksieger Initial conversion to connection pool
nicksieger authored Apr 19, 2008
143 end
62c4e4d @ebeigarts Fix connection reloading in development mode. [#4929 state:resolved]
ebeigarts authored Jun 22, 2010
144 @connections.delete_if do |conn|
145 conn.requires_reloading?
146 end
6edaa26 @nicksieger Initial conversion to connection pool
nicksieger authored Apr 19, 2008
147 end
148
817a07b @nicksieger More doco and class/method renames. Now have a strategy for integrati…
nicksieger authored Aug 8, 2008
149 # Verify active connections and remove and disconnect connections
150 # associated with stale threads.
6edaa26 @nicksieger Initial conversion to connection pool
nicksieger authored Apr 19, 2008
151 def verify_active_connections! #:nodoc:
d07a6b1 @nicksieger Make clear_active_connections! also return stale connections back to …
nicksieger authored Aug 22, 2008
152 clear_stale_cached_connections!
8e5e02b @nicksieger Collapse connection pool class hierarchy; YAGNI.
nicksieger authored Aug 22, 2008
153 @connections.each do |connection|
7ba2872 @nicksieger Deprecate verification_timeout and verify before reset
nicksieger authored Sep 4, 2008
154 connection.verify!
6edaa26 @nicksieger Initial conversion to connection pool
nicksieger authored Apr 19, 2008
155 end
156 end
157
d07a6b1 @nicksieger Make clear_active_connections! also return stale connections back to …
nicksieger authored Aug 22, 2008
158 # Return any checked-out connections back to the pool by threads that
159 # are no longer alive.
160 def clear_stale_cached_connections!
42be67e @tenderlove unfactoring clear_stale_cached_connections!
tenderlove authored Jul 16, 2010
161 keys = @reserved_connections.keys - Thread.list.find_all { |t|
162 t.alive?
163 }.map { |thread| thread.object_id }
164
165 keys.each do |key|
166 checkin @reserved_connections[key]
167 @reserved_connections.delete(key)
d07a6b1 @nicksieger Make clear_active_connections! also return stale connections back to …
nicksieger authored Aug 22, 2008
168 end
169 end
170
a293278 @lifo Merge docrails
lifo authored Oct 5, 2008
171 # Check-out a database connection from the pool, indicating that you want
172 # to use it. You should call #checkin when you no longer need this.
173 #
174 # This is done by either returning an existing connection, or by creating
175 # a new connection. If the maximum number of connections for this pool has
176 # already been reached, but the pool is empty (i.e. they're all being used),
177 # then this method will wait until a thread has checked in a connection.
178 # The wait time is bounded however: if no connection can be checked out
179 # within the timeout specified for this pool, then a ConnectionTimeoutError
180 # exception will be raised.
181 #
182 # Returns: an AbstractAdapter object.
183 #
184 # Raises:
185 # - ConnectionTimeoutError: no connection can be obtained from the pool
186 # within the timeout period.
82fcd9d @nicksieger Clean up the code, get rid of reserve/release, add some more docs
nicksieger authored Aug 7, 2008
187 def checkout
8e5e02b @nicksieger Collapse connection pool class hierarchy; YAGNI.
nicksieger authored Aug 22, 2008
188 # Checkout an available connection
189 @connection_mutex.synchronize do
21eb18a Fix race in ConnectionPool#checkout
Aliaksey Kandratsenka authored Oct 2, 2008
190 loop do
191 conn = if @checked_out.size < @connections.size
192 checkout_existing_connection
193 elsif @connections.size < @size
194 checkout_new_connection
195 end
196 return conn if conn
197 # No connections available; wait for one
198 if @queue.wait(@timeout)
199 next
200 else
8343611 made ConnectionPool#checkout more robust by trying to loot dead threa…
Aliaksey Kandratsenka authored Oct 2, 2008
201 # try looting dead threads
202 clear_stale_cached_connections!
203 if @size == @checked_out.size
dd77733 @jeremy Timeout the connection pool monitor on ruby 1.8 only
jeremy authored Nov 8, 2008
204 raise ConnectionTimeoutError, "could not obtain a database connection#{" within #{@timeout} seconds" if @timeout}. The max pool size is currently #{@size}; consider increasing it."
8343611 made ConnectionPool#checkout more robust by trying to loot dead threa…
Aliaksey Kandratsenka authored Oct 2, 2008
205 end
21eb18a Fix race in ConnectionPool#checkout
Aliaksey Kandratsenka authored Oct 2, 2008
206 end
8e5e02b @nicksieger Collapse connection pool class hierarchy; YAGNI.
nicksieger authored Aug 22, 2008
207 end
208 end
6edaa26 @nicksieger Initial conversion to connection pool
nicksieger authored Apr 19, 2008
209 end
210
a293278 @lifo Merge docrails
lifo authored Oct 5, 2008
211 # Check-in a database connection back into the pool, indicating that you
212 # no longer need this connection.
213 #
214 # +conn+: an AbstractAdapter object, which was obtained by earlier by
215 # calling +checkout+ on this pool.
8e5e02b @nicksieger Collapse connection pool class hierarchy; YAGNI.
nicksieger authored Aug 22, 2008
216 def checkin(conn)
217 @connection_mutex.synchronize do
a260e02 @wycats Whoops. _run_*_callbacks is private
wycats authored Jun 4, 2010
218 conn.send(:_run_checkin_callbacks) do
471a394 @nicksieger Modify connection pool callbacks to be compatible w/ new style
nicksieger authored Oct 16, 2009
219 @checked_out.delete conn
220 @queue.signal
221 end
8e5e02b @nicksieger Collapse connection pool class hierarchy; YAGNI.
nicksieger authored Aug 22, 2008
222 end
6edaa26 @nicksieger Initial conversion to connection pool
nicksieger authored Apr 19, 2008
223 end
82fcd9d @nicksieger Clean up the code, get rid of reserve/release, add some more docs
nicksieger authored Aug 7, 2008
224
113cc4e @nicksieger Remove some synchronization that's probably overkill, assuming one do…
nicksieger authored Aug 23, 2008
225 synchronize :clear_reloadable_connections!, :verify_active_connections!,
82fcd9d @nicksieger Clean up the code, get rid of reserve/release, add some more docs
nicksieger authored Aug 7, 2008
226 :connected?, :disconnect!, :with => :@connection_mutex
227
228 private
fe575dd @nicksieger Nearing the finish line. Initial fixed-size connection pool implement…
nicksieger authored Aug 7, 2008
229 def new_connection
a3f12f5 @nicksieger Default connection allow_concurrency to false (for PostgreSQL)
nicksieger authored Sep 4, 2008
230 ActiveRecord::Base.send(spec.adapter_method, spec.config)
fe575dd @nicksieger Nearing the finish line. Initial fixed-size connection pool implement…
nicksieger authored Aug 7, 2008
231 end
232
d07a6b1 @nicksieger Make clear_active_connections! also return stale connections back to …
nicksieger authored Aug 22, 2008
233 def current_connection_id #:nodoc:
82fcd9d @nicksieger Clean up the code, get rid of reserve/release, add some more docs
nicksieger authored Aug 7, 2008
234 Thread.current.object_id
235 end
6edaa26 @nicksieger Initial conversion to connection pool
nicksieger authored Apr 19, 2008
236
fe575dd @nicksieger Nearing the finish line. Initial fixed-size connection pool implement…
nicksieger authored Aug 7, 2008
237 def checkout_new_connection
238 c = new_connection
239 @connections << c
a96b7d4 @nicksieger Add connection reset and verification upon each connection checkout
nicksieger authored Aug 22, 2008
240 checkout_and_verify(c)
fe575dd @nicksieger Nearing the finish line. Initial fixed-size connection pool implement…
nicksieger authored Aug 7, 2008
241 end
242
243 def checkout_existing_connection
d7d2d73 @nicksieger Fix typo: was using brackets instead of parens. Must need more sleep.
nicksieger authored Aug 8, 2008
244 c = (@connections - @checked_out).first
a96b7d4 @nicksieger Add connection reset and verification upon each connection checkout
nicksieger authored Aug 22, 2008
245 checkout_and_verify(c)
246 end
247
248 def checkout_and_verify(c)
471a394 @nicksieger Modify connection pool callbacks to be compatible w/ new style
nicksieger authored Oct 16, 2009
249 c.run_callbacks :checkout do
250 c.verify!
251 @checked_out << c
252 end
fe575dd @nicksieger Nearing the finish line. Initial fixed-size connection pool implement…
nicksieger authored Aug 7, 2008
253 c
254 end
255 end
256
a293278 @lifo Merge docrails
lifo authored Oct 5, 2008
257 # 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 Jun 14, 2010
258 # for keeping separate connection pools for Active Record models that connect
a293278 @lifo Merge docrails
lifo authored Oct 5, 2008
259 # to different databases.
260 #
261 # For example, suppose that you have 5 models, with the following hierarchy:
262 #
263 # |
264 # +-- Book
265 # | |
266 # | +-- ScaryBook
267 # | +-- GoodBook
268 # +-- Author
269 # +-- BankAccount
270 #
271 # Suppose that Book is to connect to a separate database (i.e. one other
272 # than the default database). Then Book, ScaryBook and GoodBook will all use
273 # the same connection pool. Likewise, Author and BankAccount will use the
274 # same connection pool. However, the connection pool used by Author/BankAccount
275 # is not the same as the one used by Book/ScaryBook/GoodBook.
276 #
277 # 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 Jun 14, 2010
278 # ActiveRecord::Base.connection_handler. Active Record models use this to
a293278 @lifo Merge docrails
lifo authored Oct 5, 2008
279 # determine that connection pool that they should use.
ca6d717 @nicksieger Deprecate allow_concurrency and make it have no effect
nicksieger authored Aug 22, 2008
280 class ConnectionHandler
b952470 @tenderlove use an attr_reader for performance
tenderlove authored Jul 16, 2010
281 attr_reader :connection_pools
282
72d959d @nicksieger Split connection handler into single- and multiple-thread versions.
nicksieger authored Jun 7, 2008
283 def initialize(pools = {})
284 @connection_pools = pools
285 end
ff97e9d @nicksieger Connection handling methods extracted out into separate ConnectionHan…
nicksieger authored Jun 7, 2008
286
287 def establish_connection(name, spec)
8e5e02b @nicksieger Collapse connection pool class hierarchy; YAGNI.
nicksieger authored Aug 22, 2008
288 @connection_pools[name] = ConnectionAdapters::ConnectionPool.new(spec)
ff97e9d @nicksieger Connection handling methods extracted out into separate ConnectionHan…
nicksieger authored Jun 7, 2008
289 end
290
d07a6b1 @nicksieger Make clear_active_connections! also return stale connections back to …
nicksieger authored Aug 22, 2008
291 # Returns any connections in use by the current thread back to the pool,
292 # and also returns connections to the pool cached by threads that are no
293 # longer alive.
ff97e9d @nicksieger Connection handling methods extracted out into separate ConnectionHan…
nicksieger authored Jun 7, 2008
294 def clear_active_connections!
529c271 @nicksieger Simplify dispatcher callbacks to eliminate unnecessary stale thread p…
nicksieger authored Nov 8, 2008
295 @connection_pools.each_value {|pool| pool.release_connection }
ff97e9d @nicksieger Connection handling methods extracted out into separate ConnectionHan…
nicksieger authored Jun 7, 2008
296 end
297
298 # Clears the cache which maps classes
299 def clear_reloadable_connections!
300 @connection_pools.each_value {|pool| pool.clear_reloadable_connections! }
301 end
302
303 def clear_all_connections!
72d959d @nicksieger Split connection handler into single- and multiple-thread versions.
nicksieger authored Jun 7, 2008
304 @connection_pools.each_value {|pool| pool.disconnect! }
ff97e9d @nicksieger Connection handling methods extracted out into separate ConnectionHan…
nicksieger authored Jun 7, 2008
305 end
306
307 # Verify active connections.
308 def verify_active_connections! #:nodoc:
817a07b @nicksieger More doco and class/method renames. Now have a strategy for integrati…
nicksieger authored Aug 8, 2008
309 @connection_pools.each_value {|pool| pool.verify_active_connections! }
ff97e9d @nicksieger Connection handling methods extracted out into separate ConnectionHan…
nicksieger authored Jun 7, 2008
310 end
311
312 # Locate the connection of the nearest super class. This can be an
313 # active or defined connection: if it is the latter, it will be
314 # opened and set as the active connection for the class it was defined
315 # for (not necessarily the current class).
316 def retrieve_connection(klass) #:nodoc:
317 pool = retrieve_connection_pool(klass)
318 (pool && pool.connection) or raise ConnectionNotEstablished
319 end
320
82fcd9d @nicksieger Clean up the code, get rid of reserve/release, add some more docs
nicksieger authored Aug 7, 2008
321 # Returns true if a connection that's accessible to this class has
322 # already been opened.
ff97e9d @nicksieger Connection handling methods extracted out into separate ConnectionHan…
nicksieger authored Jun 7, 2008
323 def connected?(klass)
0832bc6 @lifo Make sure ActiveRecord::Base.connected? doesn't raise an exception fo…
lifo authored Nov 6, 2008
324 conn = retrieve_connection_pool(klass)
325 conn ? conn.connected? : false
ff97e9d @nicksieger Connection handling methods extracted out into separate ConnectionHan…
nicksieger authored Jun 7, 2008
326 end
327
328 # Remove the connection for this class. This will close the active
329 # connection and the defined connection (if they exist). The result
330 # can be used as an argument for establish_connection, for easily
331 # re-establishing the connection.
332 def remove_connection(klass)
333 pool = @connection_pools[klass.name]
04ef434 @tenderlove only test for existence of +pool+ once
tenderlove authored Jul 16, 2010
334 return nil unless pool
335
ff97e9d @nicksieger Connection handling methods extracted out into separate ConnectionHan…
nicksieger authored Jun 7, 2008
336 @connection_pools.delete_if { |key, value| value == pool }
04ef434 @tenderlove only test for existence of +pool+ once
tenderlove authored Jul 16, 2010
337 pool.disconnect!
338 pool.spec.config
ff97e9d @nicksieger Connection handling methods extracted out into separate ConnectionHan…
nicksieger authored Jun 7, 2008
339 end
340
fe575dd @nicksieger Nearing the finish line. Initial fixed-size connection pool implement…
nicksieger authored Aug 7, 2008
341 def retrieve_connection_pool(klass)
3007545 @nicksieger Minor tweak to retrieve_connection_pool -- recurse instead of loop
nicksieger authored Aug 26, 2008
342 pool = @connection_pools[klass.name]
343 return pool if pool
344 return nil if ActiveRecord::Base == klass
345 retrieve_connection_pool klass.superclass
fe575dd @nicksieger Nearing the finish line. Initial fixed-size connection pool implement…
nicksieger authored Aug 7, 2008
346 end
72d959d @nicksieger Split connection handler into single- and multiple-thread versions.
nicksieger authored Jun 7, 2008
347 end
1b22071 @josh Ensure ActiveRecord session store's connections are checked in after …
josh authored Feb 24, 2009
348
349 class ConnectionManagement
350 def initialize(app)
351 @app = app
352 end
353
354 def call(env)
355 @app.call(env)
356 ensure
e033b5d @lifo Merge docrails
lifo authored Jul 25, 2009
357 # Don't return connection (and perform implicit rollback) if
1b22071 @josh Ensure ActiveRecord session store's connections are checked in after …
josh authored Feb 24, 2009
358 # this request is a part of integration test
359 unless env.key?("rack.test")
360 ActiveRecord::Base.clear_active_connections!
361 end
362 end
363 end
6edaa26 @nicksieger Initial conversion to connection pool
nicksieger authored Apr 19, 2008
364 end
21eb18a Fix race in ConnectionPool#checkout
Aliaksey Kandratsenka authored Oct 2, 2008
365 end
Something went wrong with that request. Please try again.