【发布时间】:2011-02-24 19:59:18
【问题描述】:
我有一个本机查询,可以批量插入 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
。
我不关心在执行insert .. select
时从t2
读取的一致性,并希望将事务隔离级别设置为READ_UNCOMMITTED。
如何在 JPA 中进行设置?
更新
所以我最终为这种情况创建了一个常规 SQL 连接,因为在我看来这是最简单的选择。谢谢大家!
【问题讨论】:
-
我不明白它是如何导致死锁的?如果 tx1 执行: insert into t1 (a, b) select x, y from t2 where x = 'foo' 而当这种情况发生时,第二个 tx make insert 到“t2”表中,比 tx1 等待 tx2 结束,在 tx2 结束后 tx1 继续它的工作。 Dedalock 意味着 tx2 在插入 t2 时也会从 t1 读取数据?这是你的情况吗?我的意思是这样的事情会导致死锁: tx1: insert into t1 (a, b) select x, y from t2 where x = 'foo' tx2: insert into t2 (a, b) select x, y from t1 where x ='富'
标签: java mysql jpa transactions isolation-level