title: JPA and MySQL transaction isolation level
body: <p>I have a native query that does a batch insert into a MySQL database:</p>
  
  <pre><code>    String sql = "insert into t1 (a, b) select x, y from t2 where x = 'foo'";
      EntityTransaction tx = entityManager.getTransaction();
      try {
          tx.begin();
          int rowCount = entityManager.createNativeQuery(sql).executeUpdate();
          tx.commit();
          return rowCount;
      }
      catch(Exception ex) {
          tx.rollback();
          log.error(...);
      }
  </code></pre>
  
  <p>This query causes a deadlock: while it reads from <code>t2</code> with <code>insert .. select</code>, another process tries to insert a row into <code>t2</code>.</p>
  
  <p>I don't care about the consistency of reads from <code>t2</code> when doing an <code>insert .. select</code> and want to set the transaction isolation level to <a href="http://dev.mysql.com/doc/refman/5.0/en/set-transaction.html" rel="nofollow noreferrer">READ_UNCOMMITTED</a>.</p>
  
  <p>How do I go about setting it in JPA?</p>
  
  <hr>
  
  <p><strong>Update</strong></p>
  
  <p>So I ended up creating a regular SQL connection for this case as it seemed to me the simplest option. Thanks everyone!</p>