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