title: What are dirty writes? What happens if they are not allowed?
body: <p>I read about database isolation levels and transactional phenomena.
  Dirty reads are obvious but I don't understand dirty writes.</p>
  <p>All descriptions of dirty write say something like:</p>
  <blockquote>
  <p>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. <a href="https://gerardnico.com/data/property/dirty_write" rel="nofollow noreferrer">https://gerardnico.com/data/property/dirty_write</a></p>
  </blockquote>
  <p>Some other descriptions use examples to demonstrate a dirty write but not what will happen to solve that. <a href="https://esb-dev.github.io/mat/IsoLevel.pdf" rel="nofollow noreferrer">https://esb-dev.github.io/mat/IsoLevel.pdf</a></p>
  <p>This is a dirty write from the example:</p>
  <blockquote>
  <ol>
  <li>Saldo starts with 100</li>
  <li>T2 begins: update Acct set Saldo = 200 where Acct = 1</li>
  <li>T1 begins: update Acct set Saldo = 250 where Acct = 1</li>
  <li>T1 commits =&gt; Saldo=250</li>
  <li>T2 commits =&gt; Saldo=200</li>
  </ol>
  </blockquote>
  <p>I don't know what will happen when the isolation level does not allow dirty writes.</p>
  <ul>
  <li><strong>T1 fails at commit</strong> because the change of T2 isn't comitted at that time</li>
  <li>T1 commits successfully and <strong>T2 commit fails</strong> because T1 (not committed yet) overwrites that change</li>
  <li>T1 and T2 commit successfully but <strong>T1 wins</strong> silently (saldo 200 instead of 250)</li>
  </ul>
  <p>I don't know what I should expect from transaction management. The example, modified:</p>
  <ol>
  <li>Saldo starts with 100</li>
  <li>T1 begins: update Acct set Saldo = 200 where Acct = 1</li>
  <li>T2 begins: update Acct set Saldo = 250 where Acct = 1</li>
  <li>T2 commits =&gt; Saldo=?</li>
  <li>T1 commits =&gt; Saldo=?</li>
  </ol>
  <p>Do we have a dirty write here? And what is the result if dirty write is allowed?</p>
  <ul>
  <li>Saldo = 250 because the last update wins</li>
  <li>Saldo = 200 because the last commit wins</li>
  </ul>
  <p>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.</p>