| Class | Sequel::SingleThreadedPool |
| In: |
lib/sequel/connection_pool.rb
|
| Parent: | Object |
A SingleThreadedPool acts as a replacement for a ConnectionPool for use in single-threaded applications. ConnectionPool imposes a substantial performance penalty, so SingleThreadedPool is used to gain some speed.
| connection_proc | [W] | The proc used to create a new database connection |
| disconnection_proc | [RW] | The proc used to disconnect a database connection. |
Initializes the instance with the supplied block as the connection_proc.
The single threaded pool takes the following options:
# File lib/sequel/connection_pool.rb, line 217
217: def initialize(opts={}, &block)
218: @connection_proc = block
219: @disconnection_proc = opts[:disconnection_proc]
220: @conns = {}
221: @convert_exceptions = opts.include?(:pool_convert_exceptions) ? opts[:pool_convert_exceptions] : true
222: end
The connection for the given server.
# File lib/sequel/connection_pool.rb, line 225
225: def conn(server=:default)
226: @conns[server]
227: end
Disconnects from the database. Once a connection is requested using hold, the connection is reestablished.
# File lib/sequel/connection_pool.rb, line 248
248: def disconnect(&block)
249: block ||= @disconnection_proc
250: @conns.values.each{|conn| block.call(conn) if block}
251: @conns = {}
252: end
Yields the connection to the supplied block for the given server. This method simulates the ConnectionPool#hold API.
# File lib/sequel/connection_pool.rb, line 231
231: def hold(server=:default)
232: begin
233: begin
234: yield(c = (@conns[server] ||= @connection_proc.call(server)))
235: rescue Sequel::DatabaseDisconnectError => dde
236: @conns.delete(server)
237: @disconnection_proc.call(c) if @disconnection_proc
238: raise
239: end
240: rescue Exception => e
241: # if the error is not a StandardError it is converted into RuntimeError.
242: raise(@convert_exceptions && !e.is_a?(StandardError) ? RuntimeError.new(e.message) : e)
243: end
244: end