我怎么知道准备好的语句是否被缓存?
How do I know if the prepared statements are cached?
我在 SQL Server 2016 和 sqljdbc4-2 中使用 Hikari。0.jar 在 tomcat lib 文件夹中。
我的数据库资源配置如下:
<Resource name="jdbc/SQLServerDS" auth="Container" type="javax.sql.DataSource"
username="uname"
password="pwd"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="jdbc:sqlserver://server:port;DatabaseName=dbName"
maxActive="20"
maxIdle="10"
validationQuery="select 1" />
我的数据源配置如下:
@Bean(name = "dataSource")
public DataSource getDataSource() throws NamingException {
HikariConfig config = new HikariConfig();
config.setMaximumPoolSize(20);
config.setDataSourceJNDI("java:comp/env/jdbc/SQLServerDS");
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
config.addDataSourceProperty("useServerPrepStmts", "true");
config.addDataSourceProperty("cacheResultSetMetadata", "true");
config.addDataSourceProperty("useLocalSessionState", "true");
config.addDataSourceProperty("cacheServerConfiguration", "true");
config.addDataSourceProperty("elideSetAutoCommits", "true");
config.addDataSourceProperty("maintainTimeStats", "false");
return new TransactionAwareDataSourceProxy(
new LazyConnectionDataSourceProxy(new HikariDataSource(config)));
}
我如何知道准备语句缓存是否适用于不同的连接?
我正在使用 spring 容器管理事务和休眠 v4.3。10.Final。
此外,要使缓存正常工作,是否需要启用二级缓存?
HikariCP 实际上不支持PreparedStatement caching
others offer PreparedStatement caching. HikariCP does not. Why?
它被认为是错误的实现
Using a statement cache at the pooling layer is an anti-pattern, and will negatively impact your application performance compared to driver-provided caches.
解释:
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.
如果接受了,就不应该try\expect去缓存PreparedStatement
如果拒绝,可以使用C3P0作为连接池
关于Second level cache in hibernate,大多没有在连接池中定义,而是使用相关的连接提供者:
HikariCP now has a ConnectionProvider for Hibernate 4.x called HikariConnectionProvider
In order to use the HikariConnectionProvider in Hibernate 4.x add the following property to your hibernate.properties configuration file:
hibernate.connection.provider_class=com.zaxxer.hikari.hibernate.HikariConnectionProvider
As of Hibernate 4.3.6 there is an official ConnectionProvider class from Hibernate, which should be used instead of the HikariCP implementation. The class is called org.hibernate.hikaricp.internal.HikariCPConnectionProvider
对于寻找 Oracle JDBC 的任何人,
引用自 link、
Although Oracle JDBC drivers are designed with the supposition that implicit caching is enabled, this feature is not turned on by default. To enable implicit caching on the connection, you can set the implicitCachingEnabled property of the corresponding OracleConnection object to true and set the statementCacheSize property to a positive integer
要在连接池上启用它,我们需要
connectionPoolObject.setMaxStatements(10);
如果您这样做,将为池中的每个连接启用语句缓存
验证缓存是否启用,
if (conn.getImplicitCachingEnabled())
System.out.println("\nimplicit caching enabled");
else
System.out.println("\nimplicit caching disabled");
我们甚至可以在语句级别进行验证,
//Checking the creation state of the prepared statement
int creationState = stmt.creationState();
switch(creationState) {
case 0:
System.out.println("\nCreation state: new");
break;
case 1:
System.out.println("\nCreation state: from the implicit cache");
break;
case 2:
System.out.println("\nCreation state: from the explicit cache");
break;
}
当语句第一次在连接 C1 上执行时,情况 1 为真,如果同一语句在同一连接 C1 上再次执行,则情况 2 为真。
正如 user7294900 所解释的,HikariCP 不缓存准备语句。它将此任务委托给驱动程序。
Microsoft 添加了 v6.3.0-preview 中的预处理语句缓存
可以这样激活:
connection.setStatementPoolingCacheSize(10)
connection.setDisableStatementPooling(false)
这个解释很好:
我在 SQL Server 2016 和 sqljdbc4-2 中使用 Hikari。0.jar 在 tomcat lib 文件夹中。
我的数据库资源配置如下:
<Resource name="jdbc/SQLServerDS" auth="Container" type="javax.sql.DataSource"
username="uname"
password="pwd"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="jdbc:sqlserver://server:port;DatabaseName=dbName"
maxActive="20"
maxIdle="10"
validationQuery="select 1" />
我的数据源配置如下:
@Bean(name = "dataSource")
public DataSource getDataSource() throws NamingException {
HikariConfig config = new HikariConfig();
config.setMaximumPoolSize(20);
config.setDataSourceJNDI("java:comp/env/jdbc/SQLServerDS");
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
config.addDataSourceProperty("useServerPrepStmts", "true");
config.addDataSourceProperty("cacheResultSetMetadata", "true");
config.addDataSourceProperty("useLocalSessionState", "true");
config.addDataSourceProperty("cacheServerConfiguration", "true");
config.addDataSourceProperty("elideSetAutoCommits", "true");
config.addDataSourceProperty("maintainTimeStats", "false");
return new TransactionAwareDataSourceProxy(
new LazyConnectionDataSourceProxy(new HikariDataSource(config)));
}
我如何知道准备语句缓存是否适用于不同的连接?
我正在使用 spring 容器管理事务和休眠 v4.3。10.Final。
此外,要使缓存正常工作,是否需要启用二级缓存?
HikariCP 实际上不支持PreparedStatement caching
others offer PreparedStatement caching. HikariCP does not. Why?
它被认为是错误的实现
Using a statement cache at the pooling layer is an anti-pattern, and will negatively impact your application performance compared to driver-provided caches.
解释:
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.
如果接受了,就不应该try\expect去缓存PreparedStatement
如果拒绝,可以使用C3P0作为连接池
关于Second level cache in hibernate,大多没有在连接池中定义,而是使用相关的连接提供者:
HikariCP now has a ConnectionProvider for Hibernate 4.x called HikariConnectionProvider
In order to use the HikariConnectionProvider in Hibernate 4.x add the following property to your hibernate.properties configuration file:
hibernate.connection.provider_class=com.zaxxer.hikari.hibernate.HikariConnectionProvider
As of Hibernate 4.3.6 there is an official ConnectionProvider class from Hibernate, which should be used instead of the HikariCP implementation. The class is called
org.hibernate.hikaricp.internal.HikariCPConnectionProvider
对于寻找 Oracle JDBC 的任何人, 引用自 link、
Although Oracle JDBC drivers are designed with the supposition that implicit caching is enabled, this feature is not turned on by default. To enable implicit caching on the connection, you can set the implicitCachingEnabled property of the corresponding OracleConnection object to true and set the statementCacheSize property to a positive integer
要在连接池上启用它,我们需要
connectionPoolObject.setMaxStatements(10);
如果您这样做,将为池中的每个连接启用语句缓存
验证缓存是否启用,
if (conn.getImplicitCachingEnabled())
System.out.println("\nimplicit caching enabled");
else
System.out.println("\nimplicit caching disabled");
我们甚至可以在语句级别进行验证,
//Checking the creation state of the prepared statement
int creationState = stmt.creationState();
switch(creationState) {
case 0:
System.out.println("\nCreation state: new");
break;
case 1:
System.out.println("\nCreation state: from the implicit cache");
break;
case 2:
System.out.println("\nCreation state: from the explicit cache");
break;
}
当语句第一次在连接 C1 上执行时,情况 1 为真,如果同一语句在同一连接 C1 上再次执行,则情况 2 为真。
正如 user7294900 所解释的,HikariCP 不缓存准备语句。它将此任务委托给驱动程序。
Microsoft 添加了 v6.3.0-preview 中的预处理语句缓存
可以这样激活:
connection.setStatementPoolingCacheSize(10)
connection.setDisableStatementPooling(false)
这个解释很好: