I read about database isolation levels and transactional phenomena. Dirty reads are obvious but I don't understand dirty writes.
All descriptions of dirty write say something like:
A dirty write is when a process save[s to] a file data that has already been changed on disk by another process. The last process will then overwrite the data of the first one. https://gerardnico.com/data/property/dirty_write
Some other descriptions use examples to demonstrate a dirty write but not what will happen to solve that. https://esb-dev.github.io/mat/IsoLevel.pdf
This is a dirty write from the example:
- Saldo starts with 100
- T2 begins: update Acct set Saldo = 200 where Acct = 1
- T1 begins: update Acct set Saldo = 250 where Acct = 1
- T1 commits => Saldo=250
- T2 commits => Saldo=200
I don't know what will happen when the isolation level does not allow dirty writes.
- T1 fails at commit because the change of T2 isn't comitted at that time
- T1 commits successfully and T2 commit fails because T1 (not committed yet) overwrites that change
- T1 and T2 commit successfully but T1 wins silently (saldo 200 instead of 250)
I don't know what I should expect from transaction management. The example, modified:
- Saldo starts with 100
- T1 begins: update Acct set Saldo = 200 where Acct = 1
- T2 begins: update Acct set Saldo = 250 where Acct = 1
- T2 commits => Saldo=?
- T1 commits => Saldo=?
Do we have a dirty write here? And what is the result if dirty write is allowed?
- Saldo = 250 because the last update wins
- Saldo = 200 because the last commit wins
I have an additional question about Java/Spring JPA/Hibernate: Are write statements not sent to the database unless hibernate does a commit? Some isolation levels and phenomena only make sense if all statements are always transmitted instantly to the database.