Class: Sequel::SingleThreadedPool
- Defined in:
- lib/sequel/connection_pool.rb
Overview
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.
Instance Attribute Summary collapse
-
#connection_proc ⇒ Object
writeonly
The proc used to create a new database connection.
-
#disconnection_proc ⇒ Object
The proc used to disconnect a database connection.
Instance Method Summary collapse
-
#conn(server = :default) ⇒ Object
The connection for the given server.
-
#disconnect(&block) ⇒ Object
Disconnects from the database.
-
#hold(server = :default) ⇒ Object
Yields the connection to the supplied block for the given server.
-
#initialize(opts = {}, &block) ⇒ SingleThreadedPool
constructor
Initializes the instance with the supplied block as the connection_proc.
Constructor Details
#initialize(opts = {}, &block) ⇒ SingleThreadedPool
Initializes the instance with the supplied block as the connection_proc.
The single threaded pool takes the following options:
-
:disconnection_proc - The proc called when removing connections from the pool.
-
:pool_convert_exceptions - Whether to convert non-StandardError based exceptions to RuntimeError exceptions (default true)
-
:servers - A hash of servers to use. Keys should be symbols. If not present, will use a single :default server. The server name symbol will be passed to the connection_proc.
218 219 220 221 222 223 |
# File 'lib/sequel/connection_pool.rb', line 218 def initialize(opts={}, &block) @connection_proc = block @disconnection_proc = opts[:disconnection_proc] @conns = {} @convert_exceptions = opts.include?(:pool_convert_exceptions) ? opts[:pool_convert_exceptions] : true end |
Instance Attribute Details
#connection_proc=(value) ⇒ Object (writeonly)
The proc used to create a new database connection
203 204 205 |
# File 'lib/sequel/connection_pool.rb', line 203 def connection_proc=(value) @connection_proc = value end |
#disconnection_proc ⇒ Object
The proc used to disconnect a database connection.
206 207 208 |
# File 'lib/sequel/connection_pool.rb', line 206 def disconnection_proc @disconnection_proc end |
Instance Method Details
#conn(server = :default) ⇒ Object
The connection for the given server.
226 227 228 |
# File 'lib/sequel/connection_pool.rb', line 226 def conn(server=:default) @conns[server] end |
#disconnect(&block) ⇒ Object
Disconnects from the database. Once a connection is requested using #hold, the connection is reestablished.
249 250 251 252 253 |
# File 'lib/sequel/connection_pool.rb', line 249 def disconnect(&block) block ||= @disconnection_proc @conns.values.each{|conn| block.call(conn) if block} @conns = {} end |
#hold(server = :default) ⇒ Object
Yields the connection to the supplied block for the given server. This method simulates the ConnectionPool#hold API.
232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/sequel/connection_pool.rb', line 232 def hold(server=:default) begin begin yield(c = (@conns[server] ||= @connection_proc.call(server))) rescue Sequel::DatabaseDisconnectError => dde @conns.delete(server) @disconnection_proc.call(c) if @disconnection_proc raise end rescue Exception => e # if the error is not a StandardError it is converted into RuntimeError. raise(@convert_exceptions && !e.is_a?(StandardError) ? RuntimeError.new(e.) : e) end end |