SessionFactory.getCurrentSession()线程安全吗? [英] Is SessionFactory.getCurrentSession() thread safe?

查看:400
本文介绍了SessionFactory.getCurrentSession()线程安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过 SessionFactory.getCurrentSession()获取Hibernate会话是否线程安全?
假设我有一个静态SessionFactory 对象,它用于我的整个应用程序,并且有5个并发请求发送到我的servlet。我的servlet为每个请求调用方法 Auth.checkLogin(),依次 Auth.checkLogin()>通过静态 factory.getCurrentSession()获得一个会话。

Is it thread safe to obtain a Hibernate session via SessionFactory.getCurrentSession()? Suppose I have one static SessionFactory object which is used for my entire app, and I have 5 concurrent requests being made to my servlet. My servlet calls the method Auth.checkLogin() for each of those requests, and Auth.checkLogin() in turn obtains a session via the static factory.getCurrentSession().

在对我的每个请求在前面获得的会话上调用了 transaction.commit()。

At the end of each of the requests to my servlet, transaction.commit() is called on the session which was previously obtained.

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

My question is, is this a thread safe method? Or should I make any changes?

推荐答案

从Hibernate 4.0开始 SessionFactory
$ b

From Hibernate 4.0 SessionFactory


这里的主要合同是创建Session实例。 通常
应用程序具有一个SessionFactory实例
,线程
服务于客户端请求,从该工厂获取Session实例。

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.

SessionFactory的内部状态是不可变的。一旦它创建了
,就设置了这个内部状态。这个内部状态包括关于对象/关系映射的所有
元数据。

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.

实现者必须是线程安全的。

然后

So then


获得Hibernate线程安全会话通过
SessionFactory.getCurrentSession()?

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

对于api所说的话是的。

For what api says then Yes.

这篇关于SessionFactory.getCurrentSession()线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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