Class: ConnectionPool

### Home Classes Methods

### In Files

- lib/assistance/connection_pool.rb

### Parent

Object

### Methods

- ::new
- #disconnect
- #hold
- #size

### Files

- CHANGELOG
- COPYING
- README

### Class/Module Index

Quicksearch

- Validation
- Validation::ClassMethods
- Validation::Errors
- Validation::Generator
- Assistance
- Assistance::TimeCalculations
- Inflector
- Inflector::Inflections
- Array
- ConnectionPool
- FalseClass
- NilClass
- Numeric
- Object
- SingleThreadedPool
- String
- TrueClass

No matching classes.

# ConnectionPool

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

### Attributes

allocated[R]
available_connections[R]
connection_proc[RW]

The proc used to create a new connection.

created_count[R]
max_size[R]

The maximum number of connections.

mutex[R]

### Public Class Methods

new(max_size = 4, &block) click to toggle source

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 poolconnection_proc.

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

    # File lib/assistance/connection_pool.rb, line 27
    def initialize(max_size = 4, &block)
      @max_size = max_size
      @mutex = Mutex.new
      @connection_proc = block

      @available_connections = []
      @allocated = {}
      @created_count = 0
    end

### Public Instance Methods

disconnect(&block) click to toggle source

Removes all connection currently available, optionally yielding each connection to the given block. This method has the effect of disconnecting from the database. Once a connection is requested using hold, the connection pool creates new connections to the database.

    # File lib/assistance/connection_pool.rb, line 74
    def disconnect(&block)
      @mutex.synchronize do
        @available_connections.each {|c| block[c]} if block
        @available_connections = []
        @created_count = @allocated.size
      end
    end

hold() click to toggle source

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.

    # File lib/assistance/connection_pool.rb, line 52
    def hold
      t = Thread.current
      if (conn = owned_connection(t))
        return yield(conn)
      end
      while !(conn = acquire(t))
        sleep 0.001
      end
      begin
        yield conn
      ensure
        release(t)
      end
    rescue Exception => e
      # if the error is not a StandardError it is converted into RuntimeError.
      raise e.is_a?(StandardError) ? e : e.message
    end

size() click to toggle source

Returns the number of created connections.

    # File lib/assistance/connection_pool.rb, line 38
    def size
      @created_count
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.