title: Deadlock when running 2 transactional method with Spring Data and MySQL
body: <p>I am try to understand transactions using Spring Boot, Spring Data and MySQL.</p>
  <p>I created service <code>UpdateService</code> which is used to update record in database. Methods <code>change</code> and <code>change2</code> executes transactionally.</p>
  <pre><code>@Autowired
  private UserRepo userRepo;
  
  @Transactional(isolation = Isolation.SERIALIZABLE)
  public void change() {
      User user = userRepo.findOne(&quot;Jan&quot;);
  
      write(&quot;before change&quot;, user);
      sleep(2000);
      user.setPassword(&quot;new password&quot;);
      write(&quot;after change&quot;, user);
  }
  
  @Transactional(isolation = Isolation.SERIALIZABLE)
  public void change2() {
      User user = userRepo.findOne(&quot;Jan&quot;);
  
      write(&quot;before change2&quot;, user);
      user.setSecondName(&quot;new name&quot;);
      write(&quot;after change2&quot;, user);
  }
  </code></pre>
  <p>and when I call <code>change</code> and <code>change2</code> methods I received these logs:</p>
  <pre><code>before change User(name=Jan, secondName=Adam, password=Kowalski)
  before change2 User(name=Jan, secondName=Adam, password=Kowalski)
  after change2 User(name=Jan, secondName=new name, password=Kowalski)
  after change User(name=Jan, secondName=Adam, password=new password)
  </code></pre>
  <p>...and after that also I got this exception:</p>
  <pre><code>com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction
  </code></pre>
  <p>My question is this: Why does the transaction for <code>change2</code> start before <code>change</code> is done? What is the reason of deadlock?</p>