Java Reference
In-Depth Information
whether a type 2 or type 4 driver is better depends on the environment and the specific
drivers in question. There is really no a priori way to know which will perform better.
QUICK SUMMARY
1. Spend time evaluating the best JDBC driver for the application.
2. The best driver will often vary depending on the specific deployment. The same
application may be better with one JDBC driver in one deployment and a different
JDBC driver in a different deployment.
3. If there is a choice, avoid ODBC and type 1 JDBC drivers.
Prepared Statements and Statement Pooling
In most circumstances, code should use a PreparedStatement rather than a Statement for
its JDBC calls. The difference is that prepared statements allow the database to reuse inform-
ation about the SQL that is being executed. That saves work for the database on subsequent
executions of the prepared statement.
Reuse is the operative word here: the first use of a prepared statement takes more time for the
database to execute, since it must set up and save information. If the statement is used only
once, then that work will be wasted; it's better to use a regular statement in that case.
In a batch-oriented program that makes only a few database calls, the Statement interface
will let the application finish faster. But even batch-oriented programs may make hundreds
or thousands of JDBC calls to the same few SQL statements; later examples in this chapter
will use a batch program to load a database with 10,000 stock records. Batch programs that
have many JDBC calls—and application servers that will service many requests over their
lifetime—are better off using a PreparedStatement interface (and database frameworks like
JPA will do that automatically).
Prepared statements provide their performance benefit when they are pooled—that is, when
the actual PreparedStatement object is reused. For proper pooling, two things must be con-
sidered: the JDBC connection pool, and the JDBC driver configuration. (Statement pooling
is often called statement caching by database vendors.) These configuration options apply to
any program that uses JDBC, whether directly or via JPA.
Search WWH ::




Custom Search