| 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 219
219: def initialize(opts={}, &block)
220: @connection_proc = block
221: @disconnection_proc = opts[:disconnection_proc]
222: @conns = {}
223: @convert_exceptions = opts.include?(:pool_convert_exceptions) ? opts[:pool_convert_exceptions] : true
224: end
The connection for the given server.
# File lib/sequel/connection_pool.rb, line 227
227: def conn(server=:default)
228: @conns[server]
229: end
Disconnects from the database. Once a connection is requested using hold, the connection is reestablished.
# File lib/sequel/connection_pool.rb, line 250
250: def disconnect(&block)
251: block ||= @disconnection_proc
252: @conns.values.each{|conn| block.call(conn) if block}
253: @conns = {}
254: 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 233
233: def hold(server=:default)
234: begin
235: begin
236: yield(c = (@conns[server] ||= @connection_proc.call(server)))
237: rescue Sequel::DatabaseDisconnectError
238: @conns.delete(server)
239: @disconnection_proc.call(c) if @disconnection_proc
240: raise
241: end
242: rescue Exception => e
243: # if the error is not a StandardError it is converted into RuntimeError.
244: raise(@convert_exceptions && !e.is_a?(StandardError) ? RuntimeError.new(e.message) : e)
245: end
246: end