WAS v8.5 > Tune performance > Tune EJB applications > Tune applications that use the Java Persistence APIPre-loading the WSJPA ObjectCache automatically
The WebSphere Java Persistence API (WSJPA) extension to OpenJPA provides a read-only ObjectCache that can improve performance in certain use cases. By default, the data in the cache is loaded in a lazy method, which means that individual entities are loaded into memory when they are requested by an application. To load all the entities from the beginning, though, we can configure the application server to preload all of the entities from the database configured in the ObjectCache. Pre-loading the ObjectCache will allow us to cache entities that would otherwise be restricted when if you load the ObjectCache through the lazy method.
When you enable automatic loading of the ObjectCache, the JPA environment will automatically size and preload the ObjectCache instead of waiting on single entities to be requested by applications. When the application server creates the first EntityManager, the pre-loading process will start, and the application server will preload all entities from the database configured in the ObjectCache. This pre-loading process will happen asynchronously in a separate processing thread.
Be aware of the following information:
- For entities configured to be in the ObjectCache, you also need to configure all eager relationships in the ObjectCache; any lazy relationships will not be available.
- While the application server is pre-loading the ObjectCache, entities will be fetched from the database. The application server will not add any entities to the ObjectCache that are loaded by other methods.
- Auto loading the cache can take a very long time if your object graph is complex.
- Be careful when we enable this feature, because it could consume all of the available memory. For this function to work, the JPA environment needs to have the complete set of data in memory.
When the pre-loading process is complete, you will see an informational message similar to this:
Successfully loaded the ObjectCache with [...] Entities in [...] seconds.
- Set the MaxSize property to auto for the ObjectCache. For example, include the following entry in your .properties file:
<property name="wsjpa.ObjectCache" value=”true(Types=com.ibm.wsjpa.Foo; com.ibm.wsjpa.Bar, MaxSize=auto)”/>
The default value for the MaxSize property is 1000, but when we set the MaxSize property to auto the pre-loading mechanism will be enabled.
- Optional: Review the value you set for the EvictionSchedule property, which is explained in the Configure WSJPA ObjectCache to improve performance topic. When we use the EvictionSchedule property in conjunction with the MaxSize=auto setting, the EvictionSchedule property is implemented differently from the default behavior: instead of clearing the ObjectCache on the configured schedule, the ObjectCache will be automatically reloaded.
Example
The following examples show how to configure the ObjectCache to be loaded automatically:
- The following property specifies the ObjectCache will be automatically loaded when the application server creates the first EntityManager:
<property name=" wsjpa.ObjectCache" value=”true(Types=com.ibm.wsjpa.Foo; com.ibm.wsjpa.Bar, MaxSize=auto)”/>
- The following property specifies the ObjectCache will be automatically loaded when the application server creates the first EntityManager, and the ObjectCache will be reloaded every 20 minutes:
<property name=" wsjpa.ObjectCache" value=”true(Types=com.ibm.wsjpa.Foo; com.ibm.wsjpa.Bar, MaxSize=auto, EvictionSchedule=+20)”/>
Related
Configure WSJPA ObjectCache to improve performance
Develop JPA 2.x applications for a Java SE environment
Develop JPA 2.x applications for a Java EE environment
Related information: