Hello,
I am looking for a way to cache prepared statements in a servlet environment (specifically, Tomcat 5.5). This is meant to reduce the number of times that prepared statements are created, i.e. the number of times that connection.prepareStatement(sql)
is called.
My initial idea was to store PreparedStatement
objects in the session, where the key (the attribute name) is the query itself. This can also be done lazily.
However, someone alerted me to the fact that, depending on the JDBC driver implementation, the same prepared statement may be accessed by 2 threads (or requests) simultaneously, resulting, for example, in the wrong parameters being set. Therefore, the access to these statement objects needs to be synchronized.
What would be a good strategy to achieve this?
Is there a method built in to tomcat for doing this? I have see this answer where it mentions the poolPreparedStatements
DBCP parameter, but it's not entirely clear from the documentation if it carries the same meaning as what I'm looking for.