Java Database performance Best Practices
- Spend time evaluating the bestJDBC driverfor the application.
- The best driver would often vary depending on the specific deploy ment. The same application is better with one JDBC driver in one deployment and a different JDBC driver in a differ ENT deployment.
- If There is a choice,avoid ODBC and type 1 JDBC Dr Ivers.
- Java applications would typically execute the same SQL statement repeatedly. In those cases, reusing prepared statements would offer a significant performance boost.
- Prepared Statementsmust is pooled on a per-connection basis.
Most JDBC drivers and Java EE frameworks can does this automatically.
- Prepared statements can consume a significant amount of heap.
The size of the
statement pool must is carefully tuned to pre
vent GC issues from pooling too many very large objects.
- actions is expensive to commit, and the locking associated with transactions can prevent database scaling.
- transaction increases the amount of time that locks associated with the transaction is held. Especially for transactions using stricter semantics, the balance should be toward committing more frequently rather than holding the locks longer.
- for fine-grained control of transactions in JDBC, use a default transaction_read_uncommitted level and explicit ly lock data as needed.
- applications that process large amounts of data from a query should consider changing thefetch size of the data.
- there is a trade-off between loading too much data in the Appli cation (putting pressure on the garbage collector) and making frequent database calls to retrieve a set of data.
I ' ve recommended using the setfetchsize () method here on the (prepared) statement
object, but this method also exists on the ResultSet interface. In either case, the size is
just a hint. The JDBC driver is free to ignore this value, or round it to some other value,
or anything else it wants to do. There is no assurances either it, but setting the value
before the query is executed was more likely to result in the Hint being honored.
Some JDBC Drivers also allow you to set a default fetch size when the connection are
created by passing a property t o the Getconnection () method of the DriverManager.
Consult Your vendor ' s documentation if that path seems easier to manage.
Reading notes:
Java Performance:the Definitive Guide
by Scott Oaks
Copyright? Scott Oaks. All rights reserved.
Printed in the states of America.
Java Database Performance Best practices