我有一个做了批量插入到一个MySQL数据库中的原生查询:JPA和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(...);
}
这个查询将导致死锁:当它从t2
与insert .. select
读取,另一个进程试图插入排成t2
。
我不关心t2
在执行insert .. select
时读取的一致性,并且想要将事务隔离级别设置为READ_UNCOMMITTED。
如何在JPA中设置它?
更新
所以我结束了创建针对这种情况,因为在我看来,最简单的选择一个普通的SQL连接。感谢大家!
我不明白它是如何导致死锁?如果tx1执行: insert into t1(a,b)从t2中选择x,y其中x ='foo' 虽然这发生在第二个tx插入到“t2”表中,但tx1有一个等待,直到tx2结束后,tx2结束tx1继续它的工作。 Dedalock意味着tx2在插入t2的同时也会从t1读取数据?这是你的情况吗?插入t1(a,b)从t2中选择x,y其中x ='foo' tx2:插入到t2(a,b)中选择x,y从t1其中x ='foo' – Pavel 2014-06-04 21:04:56