| If caching is enabled, many actions are stored to execute cache related actions at transaction complete. These stored processes also keep reference to entity instances without needing them, thus leading to a large memory footprint for long running transactions. Even calling EntityManager.clear() has no effect on memory, because the instances are retained by these cache operations! The following method is used to check if an action needs to be registered for later processing.
protected boolean needsAfterTransactionCompletion() {
return persister.canWriteToCache() || hasPostCommitEventListeners();
}
When cache is enabled, it returns true. In case no post commit event listener is registered, the entity instance is not used in doAfterTransactionCompletion methods of EntityAction subclasses (EntityInsertAction, EntityDeleteAction, EntityUpdateAction). A separation of logic would allow to keep only the dehydrated state for the cache operation and clearing entity instance memory. |