avoid caching statements in pool_statement

Only the connections where it was prepared need to be cached.
The connections already have a statement cache based on the query
This commit is contained in:
Brian J. Cardiff 2016-08-30 16:40:52 -03:00
parent f568e4506d
commit 9c9176608d

View file

@ -6,7 +6,8 @@ module DB
class PoolStatement class PoolStatement
include StatementMethods include StatementMethods
@statements = {} of Connection => Statement # connections where the statement was prepared
@connections = Set(Connection).new
def initialize(@db : Database, @query : String) def initialize(@db : Database, @query : String)
# Prepares a statement on some connection # Prepares a statement on some connection
@ -24,7 +25,7 @@ module DB
# WHAT-IF the connection is busy? Should each statement be able to # WHAT-IF the connection is busy? Should each statement be able to
# deallocate itself when the connection is free. # deallocate itself when the connection is free.
@statements.clear @connections.clear
end end
# See `QueryMethods#exec` # See `QueryMethods#exec`
@ -58,16 +59,11 @@ module DB
end end
# builds a statement over a real connection # builds a statement over a real connection
# the conneciton and the stament is registered in `@statements` # the conneciton is registered in `@connections`
private def get_statement : Statement private def get_statement : Statement
conn, existing = @db.checkout_some(@statements.keys) conn, existing = @db.checkout_some(@connections)
if existing @connections << conn unless existing
@statements[conn] conn.prepare(@query)
else
stmt = conn.prepare @query
@statements[conn] = stmt
stmt
end
end end
end end
end end