我有一个原生查询,可以批量插入到MySQL数据库中:
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(...);
}
这个查询导致了死锁:当它使用
insert .. select
从t2
读取数据时,另一个进程试图插入一行到t2
中。在执行
insert .. select
时,我不关心对t2
的读取一致性,并希望将事务隔离级别设置为READ_UNCOMMITTED。我该如何在JPA中进行设置呢?
更新 最终我创建了一个常规SQL连接来解决这个问题,因为这似乎是最简单的选项。谢谢大家!