I have a database DB
with table T
(id
int, version
timestamp) and application APP
that works with this database and table. What I need to do:
- Lock table
T
. - Select some rows from table
T
. - Get current time
curTimeStamp
. - Unlock table
T
. - All rows thar are updated after unlocking tables (#4) must have
version
>curTimeStamp
. Theirversion
is set in UPDATE statement using SQL functions, for example,now()
,current_timestamp()
etc. I meanUPDATE T SET version=now()
;
After reading a lot of information I think about such solution:
- Start SQL transaction with
read committed
isolation level. - Execute SELECT statement to get rows of table
T
- Execute SELECT to get
curTimeStamp
. - Commit SQL transcation.
However, some RDMS, for example H2 and Posgtresql return same value of now()
functions inside one transaction. And as I understand then if some update transaction started before my select transaction AND update transaction finished after select transaction then row.version
will be < curTimeStamp
.
I am not very good in SQL transactions and I have doubts about my solution. Is this solution correct? If not, please, show my mistakes and right direction.