Java Reference
In-Depth Information
Optimizing JPA Writes
In JDBC, we looked at two critical performance techniques: reusing prepared statements and
performing updates in batches.
It is possible to accomplish both of those optimizations with JPA, but the way it is done de-
pends on the JPA implementation in use; there are no calls within the JPA API to do that. For
Java SE, these optimizations typically require setting a particular property in the applica-
tion's persistence.xml file.
One common way to optimize writes to a database is to write only those fields that have changed.
The code the HR system uses to double my salary may need to retrieve 20 fields from my em-
ployee record, but only one (very important) field needs to be written back to the database.
JPA implementations should be expected to perform this optimization transparently. This is one of
the reasons JPA bytecodes must be enhanced, since that is the process by which the JPA provider
keeps track of when values in the code are changed. When the JPA code is properly enhanced, the
SQL to write my doubled salary back to the database will update only that single column.
For example, using the JPA EclipseLink reference implementation, statement reuse is en-
abled by adding the following property to the persistence.xml file:
<property name="eclipselink.jdbc.cache-statements" value="true" />
Note that this enables statement reuse within the EclipseLink implementation. If the JDBC
driver is capable of providing a statement pool, it is usually preferable to enable the state-
ment caching in the driver and to leave this property out of the JPA configuration.
Statement batching in the reference JPA implementation is achieved by adding these proper-
<property name="eclipselink.jdbc.batch-writing" value="JDBC" />
<property name="eclipselink.jdbc.batch-writing.size" value="10000" />
JDBC drivers cannot automatically implement statement batching, so this is a useful prop-
erty to set in all cases. The batch size can be controlled in two ways: first, the size property
Search WWH ::

Custom Search