简体   繁体   English

Hibernate的会话线程安全吗?

[英]Is Hibernate's session thread safe?

I need to know, whether the Hibernate's session is thread safe or not. 我需要知道,Hibernate的会话是否是线程安全的。 But obvious a new session is attached to every thread for execution. 但很明显,每个线程都会附加一个新会话来执行。 But my question is if in one thread I have updated some value of an entity, so will that be reflected in other thread during same time execution? 但我的问题是,如果在一个线程中我更新了一个实体的某个值,那么在同一时间执行期间会反映在其他线程中吗?

My problem is when I fire update from two threads sequentially, the value is updated properly but when I fire the update almost altogether then it fails. 我的问题是当我从两个线程顺序触发更新时,值正确更新,但是当我几乎完全触发更新时,它就失败了。

for eg. 例如。 current stage of table. 现阶段的表。

  ID      NAME      MARKS
------- --------- --------
  1       John       54

I am trying to do follwing : 我想做的是:

Student student = session.load(Student.class, 1);
student.setMarks(student.getMarks() + 1);
session.update(student);
session.close();

When I try to run the above code in loop say 10, then value of "marks" in table "student" is properly updated ie the value gets updated to 64 which is proper. 当我尝试在循环中运行上面的代码10时,表“student”中的“marks”值被正确更新,即值更新为64,这是正确的。

But when I try to run the same code in threaded environment, it gives bad results. 但是当我尝试在线程环境中运行相同的代码时,它会产生错误的结果。

It is not intended that implementors be threadsafe. 并不意味着实现者是线程安全的。 Instead each thread/transaction should obtain its own instance from a SessionFactory. 相反,每个线程/事务应从SessionFactory获取自己的实例。

Even with this in mind, your behaviour might still not be what you expect, because transactions come into play. 即使考虑到这一点,您的行为可能仍然不是您所期望的,因为交易发挥作用。 You will have to set a proper transaction isolation level . 您必须设置适当的事务隔离级别 See the configuration guide , hibernate.connection.isolation property. 请参阅配置指南 hibernate.connection.isolation属性。

Hibernate session and threads do not mix. Hibernate会话和线程不混合。

You should not use a session from multiple threads at once, and I recommend you only use a session from a single thread. 您不应该同时使用多个线程的会话,我建议您仅使用单个线程中的会话。 DB session implementations are not even required to be theadsafe. 数据库会话实现甚至不需要是安全的。

You also must consider what happens to the transactions when you start doing things in multiple threads. 当你开始在多个线程中做事时,你还必须考虑事务会发生什么。 Transactions are tied to the current thread. 事务与当前线程相关联。 This becomes quickly mindblowing and you enter areas where the implementers have not tested their products. 这变得很快,你进入了实施者没有测试过他们产品的领域。

In the end life is too short to get lost in that swamp. 到最后,生命太短暂,不能迷失在沼泽中。

Hibernate sessions are not thread safe. Hibernate会话不是线程安全的。 Use TheadLocal class to create sessions for each thread:- 使用TheadLocal类为每个线程创建会话: -

 private static ThreadLocal<Session> threadSafeSession = new ThreadLocal<Session>() {
    protected Session initialValue(){
    return sf.openSession();
      }
    };

In your method get session for each thread as:- 在你的方法中,每个线程的get会话为: -

Session session = threadSafeSession.get();

It depends on how you are creating a session. 这取决于您如何创建会话。

Session can be created in two ways in hibernate. 会话可以在hibernate中以两种方式创建。

  1. getCurrentSession() 的getCurrentSession()

Yes. 是。 It offers thread safety as it'll ensure that it'll create a session for each thread if session not exist. 它提供了线程安全性,因为它确保如果会话不存在,它将为每个线程创建一个会话。 transaction and automatic session closing is attached to this. 事务和自动会话关闭附加到此。

  1. openSession() 的openSession()

It's not thread safe. 这不是线程安全的。 developer manually needs to manage transactions and session flush and close operations. 开发人员手动需要管理事务和会话刷新和关闭操作。

The Session object was designed to be used by a single thread. Session对象旨在由单个线程使用。 Internally, the Session uses many non-thread-safe data structures so it's impossible to make it thread-safe. 在内部,Session使用许多非线程安全的数据结构,因此不可能使其成为线程安全的。

More, you shouldn't even need to use a thread-safe Session . 此外,您甚至不需要使用线程安全的Session。 If your use case is to share the cached entities, then you should use the second-level cache instead which is thread-safe and can be used in a clustered environment. 如果您的用例是共享缓存的实体,那么您应该使用二级缓存,它是线程安全的,可以在集群环境中使用。

That being said, the need for having a thread-safe Session is a code smell indicating a flaw in the design of the data access layer. 话虽如此,对线程安全会话的需求是代码气味,表明数据访问层设计存在缺陷。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM