9.  Statement Batching

In addition to connection pooling and prepared statement caching, OpenJPA employs statement batching to speed up JDBC updates. Statement batching is enabled by default for any JDBC driver that supports it. When batching is on, OpenJPA automatically orders its SQL statements to maximize the size of each batch. This can result in large performance gains for transactions that modify a lot of data.

You configure statement batching through the system DBDictionary, which is controlled by the openjpa.jdbc.DBDictionary configuration property. You can enable the statement batching by setting the batchLimit in the value. The batch limit is the maximum number of statements OpenJPA will ever batch together. A value has the following meaning:

Note

By default, the batch support is based on each Dictionary to define the default batch limit. Currently only DB2 and Oracle dictionaries are set the default batch limit to 100. The default batch limit for the rest of the dictionaries is set to zero (disabled).

The example below shows how to enable and disable statement batching via your configuration properties.

Example 4.13.  Enable SQL statement batching

<property name="openjpa.jdbc.DBDictionary" value="db2(batchLimit=25)"/>
<property name="openjpa.jdbc.DBDictionary" value="oracle(batchLimit=-1)"/>
Or
<property name="openjpa.jdbc.DBDictionary" value="batchLimit=25"/>
<property name="openjpa.jdbc.DBDictionary" value="batchLimit=-1"/>

Example 4.14.  Disable SQL statement batching

<property name="openjpa.jdbc.DBDictionary" value="db2(batchLimit=0)"/>
Or
<property name="openjpa.jdbc.DBDictionary" value="batchLimit=0"/>

By default, org.apache.openjpa.jdbc.kernel.BatchingConstraintUpdateManager is the default statement batching implementation. OPENJPA also provides another update manager org.apache.openjpa.jdbc.kernel.BatchingOperationOrderUpdateManager for the statements that required ordering. You can plug-in this update manager through the "openjpa.jdbc.UpdateManager" property. Or you can plug-in your own statement batching implementation by providing the implementation that extends from AbstractUpdateManager, ConstraitUpdateManager or OperationOrderUpdateManager. Add this implementation class as a property in the persistence.xml file. For example, a custom statement batching implementation mycomp.MyUpdateManager extends ConstraitUpdateManager. You specify this implementation in the persistence.xml file as the following example:

Example 4.15.  Plug-in custom statement batching implementation

<property name="openjpa.jdbc.UpdateManager" value="mycomp.MyUpdateManager"/>