5

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?

nivcaner
  • 1,194
  • 1
  • 12
  • 22

5 Answers5

3

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.

ScArcher2
  • 85,501
  • 44
  • 121
  • 160
2

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

Robert Munteanu
  • 67,031
  • 36
  • 206
  • 278
  • I think he might be under the impression Tomcat offers this since some J2EE servlet containers will manage it for you – matt b Jun 03 '09 at 15:56
  • Is this Tomcat class not doing that? https://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/tomcat/jdbc/pool/interceptor/StatementCache.html – torahmike Jul 27 '21 at 01:33
1

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.

Mike
  • 11
  • 3
1

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

banjollity
  • 4,490
  • 2
  • 29
  • 32
0

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.

Neil Coffey
  • 21,615
  • 7
  • 62
  • 83
  • This is a question about Tomcat. So I don't see how the connection could be kept open and referenceable between requests, unless you're managing the connection pool by yourself. – Ovesh Jun 03 '09 at 18:29