Is Hibernate's session thread safe?

后端 未结 5 890
名媛妹妹
名媛妹妹 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: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.

提交回复
热议问题