* @throws SQLException if a JDBC operation fails
*/
public void testEvictionFromCache()
throws SQLException {
// Initial setup.
JDBCStatementCache cache = new JDBCStatementCache(2);
final String sql1 = "values 1";
final String sql2 = "values 2";
final String sql3 = "values 3";
// Create three physical prepares statements.
java.sql.PreparedStatement ps1 = prepareStatement(sql1);
java.sql.PreparedStatement ps2 = prepareStatement(sql2);
java.sql.PreparedStatement ps3 = prepareStatement(sql3);
// Insert the two first physical statements, the get logical wrappers.
StatementKey stmtKey1 = insertStatementIntoCache(cache, ps1, sql1);
StatementKey stmtKey2 = insertStatementIntoCache(cache, ps2, sql2);
LogicalStatementEntity logic1 =
createLogicalStatementEntity(sql1, false, cache);
LogicalStatementEntity logic2 =
createLogicalStatementEntity(sql2, false, cache);
// Insert the last physical statement and get the logical wrapper.
StatementKey stmtKey3 = insertStatementIntoCache(cache, ps3, sql3);
LogicalStatementEntity logic3 =
createLogicalStatementEntity(sql3, false, cache);
assertSame(ps1, logic1.getPhysPs());
assertSame(ps2, logic2.getPhysPs());
assertSame(ps3, logic3.getPhysPs());
// Close two first logical statements, putting them back into the cache.
logic1.close();
logic2.close();
// Assert both of the statements are open.
JDBC.assertSingleValueResultSet(ps1.executeQuery(), "1");
JDBC.assertSingleValueResultSet(ps2.executeQuery(), "2");
// Close the third statement. It should be cached, but since the cache
// will exceed its maximum capacity, the first statement will be thrown
// out and it should be closed in the process.
logic3.close();
JDBC.assertSingleValueResultSet(ps3.executeQuery(), "3");
assertNull("ps1 still in the cache", cache.getCached(stmtKey1));
try {
ps1.executeQuery();
fail("ps1 should have been closed by the cache");
} catch (SQLException sqle) {
assertSQLState("XJ012", sqle);
}
// Make sure the right statements are returned from the cache.
assertSame(ps2, cache.getCached(stmtKey2));
assertSame(ps3, cache.getCached(stmtKey3));
}