Class DataMapper::Support::ConnectionPool
In: lib/data_mapper/support/connection_pool.rb
lib/data_mapper/support/connection_pool.rb
Parent: Object

A ConnectionPool manages access to database connections by keeping multiple connections and giving threads exclusive access to each connection.

CREDIT: Sharon Rosner, maintainer of the Sequel (sequel.rubyforge.org) project an "ORM framework for Ruby" contributed this class.

Methods

hold   hold   new   new  

External Aliases

created_count -> size
created_count -> size

Attributes

allocated  [R] 
allocated  [R] 
available_connections  [R] 
available_connections  [R] 
connection_proc  [RW]  The proc used to create a new connection.
connection_proc  [RW]  The proc used to create a new connection.
created_count  [R]  Returns the number of created connections.
created_count  [R]  Returns the number of created connections.
max_size  [R]  The maximum number of connections.
max_size  [R]  The maximum number of connections.
mutex  [R] 
mutex  [R] 

Public Class methods

Constructs a new pool with a maximum size. If a block is supplied, it is used to create new connections as they are needed.

  pool = ConnectionPool.new(10) {MyConnection.new(opts)}

The connection creation proc can be changed at any time by assigning a Proc to pool#connection_proc.

  pool = ConnectionPool.new(10)
  pool.connection_proc = proc {MyConnection.new(opts)}

[Source]

    # File lib/data_mapper/support/connection_pool.rb, line 38
38:       def initialize(max_size = 4, &block)
39:         @max_size = max_size
40:         @mutex = Mutex.new
41:         @connection_proc = block
42: 
43:         @available_connections = []
44:         @allocated = {}
45:         @created_count = 0
46:       end

Constructs a new pool with a maximum size. If a block is supplied, it is used to create new connections as they are needed.

  pool = ConnectionPool.new(10) {MyConnection.new(opts)}

The connection creation proc can be changed at any time by assigning a Proc to pool#connection_proc.

  pool = ConnectionPool.new(10)
  pool.connection_proc = proc {MyConnection.new(opts)}

[Source]

    # File lib/data_mapper/support/connection_pool.rb, line 38
38:       def initialize(max_size = 4, &block)
39:         @max_size = max_size
40:         @mutex = Mutex.new
41:         @connection_proc = block
42: 
43:         @available_connections = []
44:         @allocated = {}
45:         @created_count = 0
46:       end

Public Instance methods

Assigns a connection to the current thread, yielding the connection to the supplied block.

  pool.hold {|conn| conn.execute('DROP TABLE posts;')}

Pool#hold is re-entrant, meaning it can be called recursively in the same thread without blocking.

If no connection is available, Pool#hold will block until a connection is available.

[Source]

    # File lib/data_mapper/support/connection_pool.rb, line 58
58:       def hold
59:         t = Thread.current
60:         if (conn = owned_connection(t))
61:           return yield(conn)
62:         end
63:         while !(conn = acquire(t))
64:           sleep 0.001
65:         end
66:         begin
67:           yield conn
68:         ensure
69:           release(t)
70:         end
71:       rescue Exception => e
72:         # if the error is not a StandardError it is converted into RuntimeError.
73:         raise e.is_a?(StandardError) ? e : e.message
74:       end

Assigns a connection to the current thread, yielding the connection to the supplied block.

  pool.hold {|conn| conn.execute('DROP TABLE posts;')}

Pool#hold is re-entrant, meaning it can be called recursively in the same thread without blocking.

If no connection is available, Pool#hold will block until a connection is available.

[Source]

    # File lib/data_mapper/support/connection_pool.rb, line 58
58:       def hold
59:         t = Thread.current
60:         if (conn = owned_connection(t))
61:           return yield(conn)
62:         end
63:         while !(conn = acquire(t))
64:           sleep 0.001
65:         end
66:         begin
67:           yield conn
68:         ensure
69:           release(t)
70:         end
71:       rescue Exception => e
72:         # if the error is not a StandardError it is converted into RuntimeError.
73:         raise e.is_a?(StandardError) ? e : e.message
74:       end

[Validate]