We were using hibernate ORM, we want disable the first-level cache, but We didn’t find any way.
How to disable the first-level cache?
You cannot disable the first-level cache. Why would you want to disable it?
In the same transaction，We query a data into memory,
Then modify the data using a stored procedure.
We query this data again，the result is the data before the modification.
The transaction isolation level we use is repeatable-read.
We found this to be a first-level cache issue.
If the modification is done in the same transaction, and you use Repeatable Read, then you are allowed to see your own changes.
If the modification is done using a secondary transaction, and you use Repeatable Read, then your first transaction is not allowed to see the changes done by the second transaction. This is because non-repeatable reads are prevented by the Repeatable Read isolation level.
Now, the Hibernate
EntityManager work like a transaction write behind cache which provides application-level repeatable reads for changes happening via Hibernate, not via native SQL queries.
You can circumvent the first-level cache in two ways:
- either you fetch a DTO instead of the entity
evictthe entity first and fetch it again