【问题标题】:Hibernate Save method and concurrencyHibernate Save 方法和并发
【发布时间】:2017-04-19 14:46:23
【问题描述】:

我正在使用 hibernate,想知道当多个用户请求到来时,hibernate 是如何处理的。

@Transactional 在数据层方法上。

    User user = new User();
    user.setCreatedBy("TestSad");
    user.setUsername("TestSadd");
    Integer id = (Integer) this.sessionFactory.getCurrentSession().save(user);
    // put the debug point here
    int i = 1;
    if( i ==1 ){
        // intentionally i was throwing exception as i dont want to save
        throw new NumberFormatException();
    }

对于我的测试,我打开了一个浏览器 chrome 并点击了事务,我得到的 id 值增加了 1 到 db 中存在的值。我没有关闭调试点。说 id = 10 之后,我又打开了一个浏览器 firefox 并点击了事务,我得到的 id 值加一到我在使用 chrome 浏览器调试期间得到的值。说 id = 11

下一个场景。 我已经重新启动了服务器。 再次继续上述步骤,我每次都将值增加一。说 id 12。

因此,它真正好的休眠以这种方式工作以避免出现问题。 我已经通过谷歌论坛了解它是如何工作的,但没有得到它。是hibernate如何在内部处理ACID。 有人可以帮助我理解它。只是想知道。

【问题讨论】:

标签: java hibernate


【解决方案1】:

以这种方式工作的自动增量 ID(和序列)。如果事务后来中止,它的值不会回滚。至少默认情况下。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-29
    • 2011-02-06
    • 1970-01-01
    • 2023-03-07
    • 2012-11-23
    • 2014-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多