不同Hibernate会话中对象的可见性 [英] Visibility of objects in different Hibernate sessions

查看:104
本文介绍了不同Hibernate会话中对象的可见性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FlushMode Session s被设置为 AUTO

/ code> A 会话开始

Session B 会话开始

Session A 会话创建新对象, Session#flush() 会话结束。



会话 B Session 从数据库读取对象, Session#flush()在此操作之前进行了完整的操作新创建的对象 Session A 对于 Session B也是可见的

解决方案

它取决于您的隔离级别和底层数据库。 Hibernate将隔离级别默认为底层数据库。 MySQL的默认值是REPEATABLE_READ。这意味着,如果A在B首次读取之前提交,则会话B将看到更新(不管这是否是读取更新的实体),尽管我相信这是MySQL特定的行为。有些数据库可能允许读取无关如果隔离级别是READ_COMITTED,那么只要会话A在会话B完成您感兴趣的读取之前提交,会话B就会看到来自会话A的更新。

值得指出的是,冲洗对这个问题绝对没有影响,除非你完全没有隔离。唯一重要的是提交。


FlushMode in both Sessions is set to AUTO.

Session A: Session begins

Session B: Session begins

Session A: Session creates new objects, Session#flush() is called, Session ends.

Session B: Session reads objects from database and Session#flush() is automatically performed before this operation. Will the newly created objects of Session A also be visible to Session B?

解决方案

It depends on your isolation level and the underlying database. Hibernate defaults the isolation level to the underlying database. MySQL's default is REPEATABLE_READ. This means that session B will see the update if A commits before B does its first read (regardless of whether this is a read of the updated entity in question, although I believe this is a MySQL specific behavior. Some databases may allow reads of unrelated entities).

If the isolation level is READ_COMITTED then session B will see the update from session A as long as session A commits before session B does the read you are interested in.

It's worth pointing out that flushes have absolutely no effect on this question unless you have absolutely no isolation. The only thing that matters is commits.

这篇关于不同Hibernate会话中对象的可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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