I have java-servlet application +hibernate + streaming server
Flow:
- user login begin video streaming
- decrement abonament minutes in data base every 2 seconds
- if minutes < 0 send request for the new minutes(response may be after
long time, if so continue decrement )
Problem:
Appears data consistency problem, other words while decrementing minutes,
ex: user.setMinutes(user.getMinutes() - 2) may not see changes maded by new minutes response, and minutes will be corrupted
I thought sollution could be create new hibernate transaction and commit it every time when I change minutes, but this didn’t worked:
Transaction t = session.beginTransaction();
user.setMinutes()
session.flush();
t.commit();
Question:
How to solve this problem, when each change get most recent data?
The Hibernate session caches your entities in memory (in your case the
Userentity). It’s not enough to create a new transaction for changing the remaining minutes, you also have to reload the changes made by other tasks (addditional minutes request). You can achieve this by callingrefresh():