为什么 hibernate session.close() 不会自动刷新数据?
当hibernate关闭会话时,close的目的基本上是关闭底层连接并清理一级缓存。为什么这里的刷新也不会自动发生?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从事务点来看看来,刷新非常与关闭会话不同,刷新应该发生在事务的边界内(或在提交时):
另一方面,关闭会话(和底层连接)应该在事务结束后完成(关闭连接时挂起事务的行为是 >未定义)。
因此,没有理由在 close 上做任何事情并促进不良语义,并且具有不同的操作是完全有意义的。
总结一下:
SessionFactory#getCurrentSession()
并且您不必自己Session#close()
(Session
将在提交时为您关闭时间)。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 atcommit
time):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:
SessionFactory#getCurrentSession()
and you won't have toSession#close()
yourself (theSession
will get closed for you at commit time).