本文介绍了Hibernate的sessionFactory是线程安全的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,Hibernate的会话工厂被认为是线程安全的。任何人都可以说明它是如何在Web应用程序中充当线程安全的,以及所有方法如何同步或其他内容? 解决方案

一个SessionFactory的内部状态是不可变的。大多数并发问题都是由于共享可变状态的对象。一旦对象是不可变的,它的内部状态就会在创建时被设置,并且不能被改变。



然而,Session是一个非线程安全的对象,您不能在线程之间共享它。


I learned that Hibernate's session factory is said to be thread safe. Can anyone articulate on how it acts as thread safe in a web application and how all methods are synchronized or anything else ?

解决方案

The internal state of a SessionFactory is immutable. Most problems with concurrency occur due to sharing of objects with mutable state. Once the object is immutable, its internal state is setted on creation and cannot be changed. So many threads can access it concurrently and request for sessions.

However, Session is a non-threadsafe object, you cannot share it between threads.

这篇关于Hibernate的sessionFactory是线程安全的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 08:02