gpt4 book ai didi

java - Hibernate 持久方法文档中的 "called outside of transaction"是什么意思

转载 作者:行者123 更新时间:2023-12-05 06:45:24 25 4
gpt4 key购买 nike

来自 hibernate 文档:

persist() makes a transient instance persistent. However, it does not guarantee that the identifier value will be assigned to the persistent instance immediately, the assignment might happen at flush time. persist() also guarantees that it will not execute an INSERT statement if it is called outside of transaction boundaries. This is useful in long-running conversations with an extended Session/persistence context.

这一行“persist() 还保证如果在事务边界之外调用它,它不会执行 INSERT 语句。”似乎令人困惑。如果我们调用了 persist(),那么它已经是 session 的一部分了。那么为什么会出现“交易外”的问题呢?

谢谢

最佳答案

尽管是最常用的 session 管理模式,但 session 和事务生命周期仅对于每次请求 session 相同。

但 Hibernate 是灵活的,它还支持长时间的对话,当 Session 在多个 Web 请求中被重用时。这类似于使用有状态 EJB 的 Java EE 扩展持久性上下文。

在长 session 中,当客户端线程离开服务层时,数据库连接被释放,并且 session 不再自动刷新。

这是可以在可写事务中运行的最后一个请求,它将所有状态更改同步到数据库。

关于java - Hibernate 持久方法文档中的 "called outside of transaction"是什么意思,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23874895/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com