As I understand it, each time a PreparedStatement is initialized, the statement gets cached in the memory allocated for the database connection. Therefore, if one initializes excessive PreparedStatement variables there is a risk of overflowing the memory available to the connection.
Is the cached memory freed by calling
close()on thePreparedStatementinstances?Do two
PreparedStatementcontaining identical SQL create duplicate caching events, or is the database smart enough not to cached a new instance of a duplicatePreparedStatement?
Example 1, would this overflow connection memory?:
while (true) {
PreparedStatement ps = connection.prepareStatement("SELECT id + ? FROM tbl");
ps.setDouble(1, Math.random());
ps.executeQuery();
ps.close();
}
If it would, then what about this?:
while (true) {
PreparedStatement ps = connection.prepareStatement("SELECT id FROM tbl");
ps.executeQuery();
ps.close();
}