Lock entity and refresh L1 and L2 cache in a single database call #2958
Replies: 2 comments 5 replies
-
So I think the question becomes: If we execute a query with pessimistic locking via any of should that My thought is that, well I haven't hit this issue and I'd say that is because I generally see code that is performing the select for update first ... and is not doing a prior normal select or prior 'hit L2 cache'. That said, I can easily see the desire to do that in some cases, for example hitting L2 cache first, checking some business logic before we decide to proceed and really go for the DB lock via select for update. So the use case seems good and valid.
Right, as in yes that is what we currently expect to see ... but that doesn't feel like the desirable behavior.
Yes, that doesn't seem great, does not seem to be what we want. |
Beta Was this translation helpful? Give feedback.
-
Created Issue #3020 for this |
Beta Was this translation helpful? Give feedback.
-
Hi everyone, Hi @rbygrave,
It could be interesting to have capability to refresh L1 and L2 cache while locking a row in database through
SELECT FOR UPDATE
.Indeed, it will allow us to do only one SELECT to lock and refresh the data instead of two.
At the moment, if you lock the row without doing a clear call to
refresh
, you may experience strange behaviors like the following scenario :In a transaction block :
SELECT FOR UPDATE
command, (eg. DB.lock from ENH: Add lock(bean) method as convenience for DB pessimistic locking #2362). (Do not refresh L1 and L2 cache)What we noticed is that, if the row in database is updated by another process between step 1 and step 2, the step 3 will only see old values of the Bean because it will grab it from level 2 cache set in step 1.
If we disable level 2 cache on step 3, we will still see the old value because none caches are refreshed in step 2.
If we set PersistenceContext to
QUERY
in theSELECT FOR UPDATE
of step 2, the Bean returned contains the fresh values (from database) but it do not refresh level 1 and level 2 cache neither.Solution is to call
DB.refresh
afterDB.lock
in step 2 but it imply two database query instead of one.Yours faithfully,
LCDP
Beta Was this translation helpful? Give feedback.
All reactions