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 = JDBCHelper::Constants::MAX_STATEMENT_NESTING_LEVEL) ⇒ 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 = JDBCHelper::Constants::MAX_STATEMENT_NESTING_LEVEL)
  @conn     = conn
  @max_size = max_size # TODO
  @free     = []
  @occupied = []
end

Instance Method Details

#closeObject



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

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

#eachObject



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

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

#give(stmt) ⇒ Object

Raises:

  • (Exception)


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

def give(stmt)
  return if stmt.nil?
  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
37
38
# File 'lib/jdbc-helper/connection/statement_pool.rb', line 25

def take
  if @free.empty?
    if @occupied.length >= @max_size
      raise RuntimeError.new("Statement nesting level is too deep (likely a bug)")
    end

    @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