简体   繁体   English

H2 语句池

[英]H2 statement pooling

The reason to use prepared statements are twofold:使用准备好的语句的原因有两个:

  1. Prevent SQL injection by adding data from the outside world only as parameters.通过仅将来自外界的数据作为参数添加来防止 SQL 注入。
  2. Improve performance by letting the DB pre-parse the statement once and re-use it many times by just passing the parameters.通过让数据库预解析语句一次并通过传递参数多次重复使用它来提高性能。

But prepared statements in JDBC are tied in with a connection.但是 JDBC 中的预处理语句与连接相关联。 And we should use connection pooling.我们应该使用连接池。 This means in a typical code sample I have to这意味着在典型的代码示例中我必须

  • obtain a connection from the pool从池中获取连接
  • (re)-create the prepared statement every time, because there is no other sensible way to get it back (re)-每次都创建准备好的语句,因为没有其他明智的方法可以取回它

When using JdbcConnectionPool, the simple connection pool that comes with H2, I think statements are not cached.在使用H2自带的简单连接池JdbcConnectionPool的时候,我觉得语句是没有缓存的。 Does it mean there is rather a negative impact on performance when using prepared statements --- which I can not really avoid due to (1) above.这是否意味着在使用准备好的语句时会对性能产生相当负面的影响——由于上面的(1),我无法真正避免。

H2 caches prepared statements transparently on the connection level. H2 在连接级别上透明地缓存准备好的语句。 Meaning, as long as the SQL string is the same when you create a prepared statement and you are using the same connection it is not re-parsed , but only compared as opaque string for the cache lookup (see org.h2.jdbc.JdbcConnection#prepareCommand(...) ).意思是,只要在您创建准备好的语句并且使用相同的连接时,SQL 字符串是相同的,它就不会重新解析,而只是作为缓存查找的不透明字符串进行比较(请参阅org.h2.jdbc.JdbcConnection#prepareCommand(...) )。

You can configure the query cache size with the parameter QUERY_CACHE_SIZE —more info here .您可以使用参数QUERY_CACHE_SIZE配置查询缓存大小 — 更多信息请点击此处

Depending on your usage scenario, you might want to experiment with not using a connection pool at all.根据您的使用场景,您可能想要尝试完全不使用连接池。 Eg as embedded database, you don't really have high connection costs.例如,作为嵌入式数据库,您实际上并没有很高的连接成本。 You might experience a greater performance gain when using only a single connection (more cache hits; lower memory consumption, because you don't duplicate your caches).仅使用单个连接时,您可能会体验到更大的性能提升(更多的缓存命中;更低的内存消耗,因为您不复制缓存)。 Also, many parts of H2 aren't (AFAIK) really built for concurrency.此外,H2 的许多部分(AFAIK)并不是真正为并发而构建的。

So to answer your question:所以回答你的问题:

No, I don't think here is a negative impact per se on prepared statement performance.不,我认为这本身不会对准备好的语句性能产生负面影响。 To the contrary.从相反的方面来说。

However, think about what kind of pool you want to use and how big you want it to be.但是,请考虑您要使用哪种类型的池以及您希望它有多大。 Also, what kind of concurrency do you expect?另外,您期望什么样的并发? Then measure both speed and memory consumption in the kind of scenario you expect to see in your live application.然后在您希望在实时应用程序中看到的场景中测量速度和内存消耗。 And... think about how relevant this really is for your application.并且......想想这对您的应用程序有多重要。 I suspect there's probably bigger fish to fry elsewhere.我怀疑其他地方可能有更大的鱼要炸。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM