Hikari CP 是否支持 PreparedStatements 缓存?

Does Hikari CP support PreparedStatements cache?

我在这个 Whosebug answer() 上看到 hikari 不支持 PreparedStatement 缓存。

我在Hikari CP文档里也看到了

Many connection pools, including Apache DBCP, Vibur, c3p0 and others offer PreparedStatement caching. HikariCP does not. Why?

但是,在Hikari cp文档的示例代码中设置了prepared statement cache。 (https://github.com/brettwooldridge/HikariCP#rocket-initialization)

HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:mysql://localhost:3306/simpsons");
config.setUsername("bart");
config.setPassword("51mp50n");
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");

HikariDataSource ds = new HikariDataSource(config);

Hikari说不支持缓存,为什么可以配置缓存? 上面的cachePrepStmts是什么意思?

Many connection pools, including Apache DBCP, Vibur, c3p0 and others offer PreparedStatement caching. HikariCP does not. Why?

Hikari 本身 没有support/provide 缓存,它将缓存委托给正在使用的基础数据源,在本例中为MySQL。这些属性直接设置在底层 MySQL 数据源上以进行缓存。例如,这些设置在 Postgres 上不起作用(它们在那里有不同的名称)。 Hikari“只是”一个连接池,仅此而已,仅此而已,它委托给底层使用的 datasource/jdbc 驱动程序的所有其他内容。

其中,如果您阅读 the documentation 也是其中的解释。

At the connection pool layer PreparedStatements can only be cached per connection. If your application has 250 commonly executed queries and a pool of 20 connections you are asking your database to hold on to 5000 query execution plans -- and similarly the pool must cache this many PreparedStatements and their related graph of objects.

Most major database JDBC drivers already have a Statement cache that can be configured, including PostgreSQL, Oracle, Derby, MySQL, DB2, and many others. JDBC drivers are in a unique position to exploit database specific features, and nearly all of the caching implementations are capable of sharing execution plans across connections. This means that instead of 5000 statements in memory and associated execution plans, your 250 commonly executed queries result in exactly 250 execution plans in the database. Clever implementations do not even retain PreparedStatement objects in memory at the driver-level but instead merely attach new instances to existing plan IDs.

这是文档中的部分,突出显示的部分准确解释了为什么 Hikari 本身不提供缓存而是将其委托给正在使用的驱动程序。