advancedThe Persistence Context — First-Level Cache, Dirty Checking & Flush
Why can a long-running batch job that processes thousands of entities in one transaction run into memory problems?
The persistence context holds every loaded/managed entity (plus its load-time snapshot for dirty checking) for the entire life of the unit of work — processing thousands of entities in one transaction with no cleanup means all of them accumulate in memory simultaneously. The fix is periodically calling flush() followed by clear() to sync pending changes and release the accumulated entities from the persistence context.
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response