4

I want to clear cache data from 2nd level cache by calling below method:

sessionFactory.getCache().evictEntityRegions();

I just want to know , is there any harm in doing this? For example: What will happen if I try to clear cache in middle of transaction?

Thanks in advance.

Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63

1 Answers1

2

No, there is no harm in doing that. Effectively, the same thing happens when you configure the cache provider to evict/expire items based on the defined eviction/expiration policy.

Dragan Bozanovic
  • 23,102
  • 5
  • 43
  • 110
  • Hi Dragan, I have a case.Suppose if some transaction is running to get Data from and 2nd level cache has data at that time. At the same time, another thread evicts all region caches while the previous transaction is not completed yet. Then what will happen in this case. Can i get null from cache in that transaction and a db hit will occur ? Is there any chance of any issue? – Vikas Sharma Feb 17 '17 at 04:48
  • 1
    @VikasVats In that case, object is already in the first level cache (session) and will be fetched from there by the end of the transaction. If the object is manually evicted from the session or entire session is manually cleared, then the object would be simply reloaded from the db and stored in both first- and second-level caches. There is no chance of any issue regarding that. – Dragan Bozanovic Feb 17 '17 at 19:45