Class: ODBA::ConnectionPool

Inherits:
Object show all
Defined in:
lib/odba/connection_pool.rb

Constant Summary collapse

POOL_SIZE =
5
SETUP_RETRIES =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*dbi_args) ⇒ ConnectionPool

All connections are delegated to DBI. The constructor simply records the DBI-arguments and reuses them to setup connections when needed.



17
18
19
20
21
22
23
# File 'lib/odba/connection_pool.rb', line 17

def initialize(*dbi_args)
	@dbi_args = dbi_args
    @opts = @dbi_args.last.is_a?(Hash) ? @dbi_args.pop : Hash.new
	@connections = []
	@mutex = Mutex.new
	connect
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

:nodoc:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/odba/connection_pool.rb', line 35

def method_missing(method, *args, &block) # :nodoc:
	tries = SETUP_RETRIES
	begin
		next_connection { |conn|
			conn.send(method, *args, &block)
		}
	rescue NoMethodError, DBI::Error => e
      warn e
		if(tries > 0 && (!e.is_a?(DBI::ProgrammingError) \
         || e.message == 'no connection to the server'))
			sleep(SETUP_RETRIES - tries)
			tries -= 1
			reconnect
			retry
		else
			raise
		end
	end
end

Instance Attribute Details

#connectionsObject (readonly)

attr_reader :connections



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

def connections
  @connections
end

#dbi_argsObject (readonly)

attr_reader :connections



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

def dbi_args
  @dbi_args
end

Instance Method Details

#_connectObject

:nodoc:



61
62
63
64
65
66
67
68
69
# File 'lib/odba/connection_pool.rb', line 61

def _connect # :nodoc:
  POOL_SIZE.times {
    conn = DBI.connect(*@dbi_args)
    if encoding = @opts[:client_encoding]
      conn.execute "SET CLIENT_ENCODING TO '#{encoding}'"
    end
    @connections.push(conn)
  }
end

#_disconnectObject

:nodoc:



73
74
75
76
77
78
79
80
81
82
# File 'lib/odba/connection_pool.rb', line 73

def _disconnect # :nodoc:
	while(conn = @connections.shift)
		begin 
			conn.disconnect
		rescue DBI::InterfaceError, Exception
			## we're not interested, since we are disconnecting anyway
        nil
		end
	end
end

#connectObject

:nodoc:



58
59
60
# File 'lib/odba/connection_pool.rb', line 58

def connect # :nodoc:
	@mutex.synchronize { _connect }
end

#disconnectObject

:nodoc:



70
71
72
# File 'lib/odba/connection_pool.rb', line 70

def disconnect # :nodoc:
	@mutex.synchronize { _disconnect }
end

#next_connectionObject

:nodoc:



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/odba/connection_pool.rb', line 24

def next_connection # :nodoc:
	conn = nil
	@mutex.synchronize {
		conn = @connections.shift
	}
	yield(conn)
ensure
	@mutex.synchronize {
		@connections.push(conn)
	}
end

#reconnectObject

:nodoc:



83
84
85
86
87
88
# File 'lib/odba/connection_pool.rb', line 83

def reconnect # :nodoc:
	@mutex.synchronize {
		_disconnect
		_connect
	}
end

#sizeObject Also known as: pool_size



54
55
56
# File 'lib/odba/connection_pool.rb', line 54

def size 
	@connections.size
end