Search code examples
javacachingtransactionspersistencetransactional

Transactional & Non-Transactional Cache in Java Persistence


What is transactional cache and non-transactional cache?

And what is the difference between them?


Solution

  • JPA supports different transaction isolation levels for caches which can be commonly classified into transactional and non-transactional.

    Transaction isolation defines how the changes that are made by one operation become visible to other concurrent operations.

    In a transactional cache, the changes from a transaction are committed to the cache as a single atomic unit. This means the objects/data are first locked in the cache (preventing other threads/users from accessing the objects/data), then updated in the cache, then the locks are released. Ideally the locks are obtained before committing the database transaction, to ensure consistency with the database.

    In a non-transactional cache the objects/data are updated one by one without any locking. This means there will be a brief period where the data in the cache is not consistent with the database.

    Some JPA providers may allow for configuration of their cache isolation to support fine-grained control over the consistency and isolation of the caches.