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 = 20) ⇒ StatementPool
constructor
A new instance of StatementPool.
- #take ⇒ Object
- #with ⇒ Object
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
#close ⇒ Object
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 |
#each ⇒ Object
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
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 |
#take ⇒ Object
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 |
#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 |