gpt4 book ai didi

java - SessionFactory.getCurrentSession() 线程安全吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:29:12 24 4
gpt4 key购买 nike

通过 SessionFactory.getCurrentSession() 获取 Hibernate session 是否线程安全?假设我有一个 static SessionFactory 对象用于我的整个应用程序,并且我有 5 个并发请求正在向我的 servlet 发出。我的 servlet 为每个请求调用方法 Auth.checkLogin(),然后 Auth.checkLogin() 依次通过静态 factory.getCurrentSession 获取 session ()

在对我的 servlet 的每个请求结束时,transaction.commit() 将在之前获得的 session 上调用。

我的问题是,这是线程安全的方法吗?或者我应该做任何改变吗?

最佳答案

来自 Hibernate 4.0 SessionFactory

The main contract here is the creation of Session instances. Usually an application has a single SessionFactory instance and threads servicing client requests obtain Session instances from this factory.

The internal state of a SessionFactory is immutable. Once it is created this internal state is set. This internal state includes all of the metadata about Object/Relational Mapping.

Implementors must be threadsafe.

那么

Is it thread safe to obtain a Hibernate session via SessionFactory.getCurrentSession()?

对于 api 所说的,是的。

关于java - SessionFactory.getCurrentSession() 线程安全吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22030448/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com