每个连接缓存PreparedStatement还是让连接池处理它?

Gil*_*ili 5 connection-pooling jdbc prepared-statement

哪种缓存策略更快,速度是多少?

1)PreparedStatement池(通过连接池).应用程序没有缓存.

for (int i=0; i<1000; i++) {
    PreparedStatement preparedStatement = connection.prepareStatement(sql);
    preparedStatement.setObject(1, someValue);
    preparedStatement.executeQuery();
    preparedStatement.close();
}
Run Code Online (Sandbox Code Playgroud)

2)应用程序级缓存.没有PreparedStatement汇集.

PreparedStatement preparedStatement = connection.prepareStatement(sql);
for (int i=0; i<1000; i++) {
    preparedStatement.clearParameters();
    preparedStatement.setObject(1, someValue);
    preparedStatement.executeQuery();
}
preparedStatement.close();
Run Code Online (Sandbox Code Playgroud)

这个问题类似于多次重用PreparedStatement,除了我期待具体的基准测试结果以及考虑PreparedStatement池.

http://drupal.org/node/550124#comment-2224630似乎表明应用程序级缓存比PreparedStatement池更有效,但差异可以忽略不计.我想在做出决定之前看到更多基准.

tri*_*ogy 0

应用程序级缓存会更加有效,特别是如果您批量执行这些操作。

即使使用连接池,每次准备好连接(返回和第四次确认)和关闭连接所需的网络开销不仅会降低速度,还会增加 SQL 服务器和客户端服务器上的 CPU 级别成本。