Class: JDBCHelper::Connection::StatementPool
- Inherits:
-
Object
- Object
- JDBCHelper::Connection::StatementPool
- Defined in:
- lib/jdbc-helper/connection/statement_pool.rb
Instance Method Summary collapse
- #close ⇒ Object
- #each ⇒ Object
- #give(stmt) ⇒ Object
-
#initialize(conn, max_size = JDBCHelper::Constants::MAX_STATEMENT_NESTING_LEVEL) ⇒ StatementPool
constructor
A new instance of StatementPool.
- #take ⇒ Object
- #with ⇒ Object
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
#close ⇒ Object
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 |
#each ⇒ Object
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
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 |
#take ⇒ Object
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 |
#with ⇒ Object
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 |