【问题标题】:hibernate : select for update in two requesthibernate:在两个请求中选择更新
【发布时间】:2026-02-12 15:10:01
【问题描述】:

在我的springBoot 项目中,我使用hibernate 在我的oracle 数据库上获取lock

在我的交易中,我执行以下代码:

Query q = em.createQuery("SELECT m FROM myTablem WHERE foo= :cat");
q.setParameter("cat", myValue);
q.setMaxResults(1);

//use pessimistic lock
//With Oracle, the select is a 'select for update' request
q.setLockMode(LockModeType.PESSIMISTIC_WRITE);

//set hibernate timeout to 0
//With Oracle, the 'select for update' is transformed in 'select for update nowait'
q.setHint("javax.persistence.lock.timeout", 0);

List<Msisdn> msisdns = q.getResultList();

我想在我的数据库上执行"select for update"。 但是使用这段代码,hibernate 会执行两个请求:

休眠:select * from (select m..... from myTablem m cross join categorie_critere categoriec1_ where m.foo=...) where rownum

休眠:从 idpk1 =? 的 m 中选择 ce_code_abpq和 idpk2 =?和 idpk3 =?等待更新

在第一个请求中,hibernate 执行select 请求。在第二个请求中,hibernate 对选定元素调用 "select for update"

是否可以选择仅在一个请求中执行"select for update"

问候 塞德里克

【问题讨论】:

    标签: java hibernate jpa


    【解决方案1】:

    禁用后续锁定:

    1. 升级到休眠 5.2.1
    2. 要么将数据库升级到 12c 并使用Oracle12cDialect,要么将HINT_FOLLOW_ON_LOCKING 查询提示设置为false

    请参阅here 了解更多信息。

    【讨论】:

      最近更新 更多