Class: JDBCHelper::Connection::StatementPool

Inherits:
Object
  • Object
show all
Defined in:
lib/jdbc-helper/connection/statement_pool.rb

Instance Method Summary collapse

Constructor Details

#initialize(conn, max_size = 20) ⇒ StatementPool

Returns a new instance of StatementPool.



10
11
12
13
14
15
# File 'lib/jdbc-helper/connection/statement_pool.rb', line 10

def initialize(conn, max_size = 20)
	@conn = conn
	@max_size = max_size # TODO
	@free = []
	@occupied = []
end

Instance Method Details

#closeObject



46
47
48
49
50
51
52
53
# File 'lib/jdbc-helper/connection/statement_pool.rb', line 46

def close
	(@free + @occupied).each do | stmt |
		stmt.close
	end
	@conn = nil
	@free = []
	@occupied = []
end

#eachObject



55
56
57
58
59
# File 'lib/jdbc-helper/connection/statement_pool.rb', line 55

def each
	(@free + @occupied).each do | stmt |
		yield stmt
	end
end

#give(stmt) ⇒ Object

Raises:

  • (Exception)


38
39
40
41
42
43
44
# File 'lib/jdbc-helper/connection/statement_pool.rb', line 38

def give(stmt)
	raise Exception.new("Not my statement") unless
			@occupied.include? stmt

	@occupied.delete stmt
	@free << stmt
end

#takeObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/jdbc-helper/connection/statement_pool.rb', line 25

def take
	if @free.empty?
		raise Exception.new("Statement nesting level is too deep (likely a bug)") if
				@occupied.length >= @max_size
		@occupied << nstmt = @conn.send(:create_statement)
		nstmt
	else
		stmt = @free.pop
		@occupied << stmt
		stmt
	end
end

#withObject



17
18
19
20
21
22
23
# File 'lib/jdbc-helper/connection/statement_pool.rb', line 17

def with
	begin
		yield stmt = take
	ensure
		give stmt
	end
end