Second level hibernate caching - Making object available at session factory level?

75 Views Asked by At

I have some understanding about how this first level and second level caching is used in hibernate. There are lot of questions related to this- yes I agree

But my confusion is. In first level caching , until unless I commit the transaction, my data will not be persisted in db and so other session will not come to know this changes before that. If second level caching brings entities to session factory level! Does it mean my changes in one session can b used by other sessions even before I commit the transaction??

And when will the update to dB happens while using second level caching ?? in 1st it will happen while ending transaction

I referred many discussions in this topic and I don’t find exact answers!!

1

There are 1 best solutions below

0
On

Your updates are definitely not shared with other sessions until you commit the transaction. If something like this happens, it is a serious bug.

There is no difference in the DB commit with or without 2LC, besides the fact that 2LC may fail the transaction before DB is fully committed. Both JPA and Hibernate's non-JPA transactions excercise 2-phase commit where all resources are first notified that transaction is being committed, acquire some locks etc. and if all resources succeed the changes are persisted/published.

Transactional cache is not too different from a regular DB: it pretends that the changes have been already applied but keeps them hidden from other transactions; this is called multiversion concurrency control (MVCC).