为什么每个应用程序只使用一个 SessionFactory 对象? [英] Why use only one SessionFactory Object per application?

查看:34
本文介绍了为什么每个应用程序只使用一个 SessionFactory 对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么每个应用程序只使用一个 SessionFactory 对象?每个应用程序使用单个会话工厂对象有什么好处?

Why use only one SessionFactory Object per application? What are the advantages of using single session factory object per application?

推荐答案

会话工厂对象将使用 singleton 设计模式来实现.SessionFactory 的实例是线程安全的,并且通常在整个应用程序中共享.由于这些对象很重,因为它们包含连接信息、hibernate 配置信息和映射文件、位置路径.因此,创建实例数量将使我们的应用程序重量.但是会话对象不是线程安全的.所以简而言之 - SessionFactory 对象每个应用程序一个,Session 对象每个客户端一个.

Session factory objects are to be implemented using the singleton design pattern. Instances of SessionFactory are thread-safe and typically shared throughout an application. As these objects are heavy weight because they contains the connection information, hibernate configuration information and mapping files,location path. So creating number of instances will make our application heavy weight. But the session objects are not thread safe. So in short it is - SessionFactory objects are one per application and Session objects are one per client.

因此,每个数据源将是一个 SessionFactory.您的应用程序可能有多个数据源,因此在该实例中您可能有多个 SessionFactory.但是您不希望在应用程序中多次创建 SessionFactory.

Hence it would be one SessionFactory per DataSource. Your application may have more than one DataSource so you may have more than one SessionFactory in that instance. But you would not want to create a SessionFactory more than once in an application.

优势:显然它提高了您的应用程序的性能:)

Advantages: Obviously its improving performance of your application :)

更新-摘自 Hibernate Doc

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.

这篇关于为什么每个应用程序只使用一个 SessionFactory 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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