Is Hibernate's session thread safe?

后端 未结 5 885
名媛妹妹
名媛妹妹 2020-12-06 00:01

I need to know, whether the Hibernate\'s session is thread safe or not. But obvious a new session is attached to every thread for execution. But my question is if in one thr

相关标签:
5条回答
  • 2020-12-06 00:45

    Hibernate session and threads do not mix.

    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.

    0 讨论(0)
  • 2020-12-06 00:52

    It is not intended that implementors be threadsafe. Instead each thread/transaction should obtain its own instance from a 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.

    0 讨论(0)
  • 2020-12-06 00:55

    It depends on how you are creating a session.

    Session can be created in two ways in hibernate.

    1. 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()

    It's not thread safe. developer manually needs to manage transactions and session flush and close operations.

    0 讨论(0)
  • Hibernate sessions are not thread safe. Use TheadLocal class to create sessions for each thread:-

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

    In your method get session for each thread as:-

    Session session = threadSafeSession.get();
    
    0 讨论(0)
  • 2020-12-06 00:59

    The Session object was designed to be used by a single thread. Internally, the Session uses many non-thread-safe data structures so it’s impossible to make it thread-safe.

    More, you shouldn’t even need to use a thread-safe 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.

    0 讨论(0)
提交回复
热议问题