title: JPA two transaction updating the same rows
body: <p>I'm using JPA repository in my SpringBoot app I have a background task that periodically set certain element in my database to 'ready'
  I also have an endpoint that a user can call and that can modify the one of the row of that same table.</p>
  
  <p>Is there a way to avoid one and another to cancel each other writes? Take this scenario</p>
  
  <pre><code>Table: 
  Key
  id     name      is_ready
  </code></pre>
  
  <p>0) Initial data there a key (1     no_name     false)</p>
  
  <p>1) the background task kicks in and is about to modify the table Key by setting </p>
  
  <pre><code>is_ready to true
  Key key = repo.findKeyByIsReady(false)
  key.setIsReady(true)
  repo.save(key) &lt;--- does NOT yet execute this
  </code></pre>
  
  <p>2) a user call the api endpoint to change a key name to "new_name" and complete</p>
  
  <p>3) now the background service execute the repo.save(key) and the final data is</p>
  
  <pre><code>1    no_name   true 
  </code></pre>
  
  <p>instead of </p>
  
  <pre><code>1    new_name   true
  </code></pre>
  
  <p>basically the background task has overwritten the key name set by the user</p>
  
  <p>is there a way to avoid this kind of scenario? How are transaction going to be helpful here?</p>