PreparedStatement caching in Tomcat

2019-05-02 16:10发布

I am looking for a way to achieve PreparedStatement caching, so as to save recreating the PreparedStatement objects for queries that were already executed in the past.

Is there some built in way to achieve this using Tomcat? Or must I program this cache myself?

5条回答
Summer. ? 凉城
2楼-- · 2019-05-02 16:27

Perhaps I'm missing something in what you're asking, but if you're making your queries in "raws" JDBC, then essentially all you need to do is keep the connection open and keep on to a reference to the PreparedStatement.

查看更多
对你真心纯属浪费
3楼-- · 2019-05-02 16:29

Prepared statement caching is done by either your JDBC connection pool or your JDBC driver, not by Tomcat.

查看更多
趁早两清
4楼-- · 2019-05-02 16:41

I believe tomcat uses commons-dbcp and commons-dbcp supports prepared statement pooling.

check it out here.

poolPreparedStatements false Enable prepared statement pooling for this pool.

查看更多
Melony?
5楼-- · 2019-05-02 16:48

You don't state your database, but if it's SQL Server then the jTDS driver does this internally for you. It's all abstracted away so you don't need to write any hairy caching code.

See here: http://jtds.sourceforge.net/faq.html#preparedStatmentMemoryLeak

查看更多
劫难
6楼-- · 2019-05-02 16:52

DB2 JDBC Driver (JCC) uses a JDBC connection property like maxStatements=10; and it does cache the statements for the connection. The pool need not cache them.

查看更多
登录 后发表回答