Class: DataMapper::Support::ConnectionPool

Inherits:
Object
  • Object
show all
Defined in:
lib/data_mapper/support/connection_pool.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_size = 4, &block) ⇒ ConnectionPool

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)}


34
35
36
37
38
39
40
41
42
# File 'lib/data_mapper/support/connection_pool.rb', line 34

def initialize(max_size = 4, &block)
  @max_size = max_size
  @mutex = Mutex.new
  @connection_proc = block

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

Instance Attribute Details

#allocatedObject (readonly)

Returns the value of attribute allocated.



22
23
24
# File 'lib/data_mapper/support/connection_pool.rb', line 22

def allocated
  @allocated
end

#available_connectionsObject (readonly)

Returns the value of attribute available_connections.



22
23
24
# File 'lib/data_mapper/support/connection_pool.rb', line 22

def available_connections
  @available_connections
end

#connection_procObject

The proc used to create a new connection.



20
21
22
# File 'lib/data_mapper/support/connection_pool.rb', line 20

def connection_proc
  @connection_proc
end

#created_countObject (readonly)

Returns the value of attribute created_count.



22
23
24
# File 'lib/data_mapper/support/connection_pool.rb', line 22

def created_count
  @created_count
end

#max_sizeObject (readonly)

The maximum number of connections.



17
18
19
# File 'lib/data_mapper/support/connection_pool.rb', line 17

def max_size
  @max_size
end

#mutexObject (readonly)

Returns the value of attribute mutex.



14
15
16
# File 'lib/data_mapper/support/connection_pool.rb', line 14

def mutex
  @mutex
end

Instance Method Details

#holdObject

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.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/data_mapper/support/connection_pool.rb', line 59

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

#sizeObject

Returns the number of created connections.



45
46
47
# File 'lib/data_mapper/support/connection_pool.rb', line 45

def size
  @created_count
end