为什么hibernate session.close()不会自动刷新数据? [英] Why hibernate session.close() does not flushes the data automatically?

查看:358
本文介绍了为什么hibernate session.close()不会自动刷新数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当hibernate关闭一个会话时,关闭的目的基本上是关闭底层连接并清理一级缓存。为什么flush也不会自动发生在这里?

解决方案

transactional 的观点, flushing 与关闭会话和 flush 应该发生在事务的边界内(或者在 commit 时间):


结束会话通常涉及四个
不同阶段:
$ b $ ul

  • 刷新会话
  • 提交交易

  • 关闭会话

  • 处理异常情况


  • 另一方面,关闭一个Session(和底层连接)应该在一个事务结束后完成关闭连接时挂起事务的行为是未定义)。

    因此,没有理由关闭任何事情并促成不良的语义,并且具有不同的操作是非常合理的。



    总结:


    1. just 使用事务并且正确(如果需要,会话将在提交时刷新,具体取决于FlushMode)。

    2. 使用 SessionFactory#getCurrentSession(),您不必为 Session#close() code>你自己(在提交时 Session 会在你提交时关闭)。


    When hibernate closes a session, the purpose of close is basically to close the underlying connection and the clean up the first level cache. Why the flush also does not happens automatically here?

    解决方案

    From a transactional point of view, flushing is very different from closing the session and flush should occur inside the boundaries of a transaction (or at commit time):

    Ending a Session usually involves four distinct phases:

    • flush the session
    • commit the transaction
    • close the session
    • handle exceptions

    On the other hand, closing a Session (and the underlying connection) should be done after a transaction has ended (the behavior of a pending transaction when closing a connection is undefined).

    There is thus no reason to do anything on close and to promote bad semantics and it makes perfect sense to have distinct operations.

    To sum up:

    1. just use a transaction and proper demarcation as you're supposed to (and the session will get flushed at commit time if required, depending on the FlushMode).
    2. use SessionFactory#getCurrentSession() and you won't have to Session#close() yourself (the Session will get closed for you at commit time).

    这篇关于为什么hibernate session.close()不会自动刷新数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆