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

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

问题描述

为什么每个应用程序只使用一个SessionFactory对象?每个应用程序使用单个会话工厂对象的优点是什么?

解决方案

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

因此,它将是每个DataSource一个SessionFactory。您的应用程序可能有多个DataSource,因此您可能在该实例中拥有多个SessionFactory。但您不希望在应用程序中多次创建SessionFactory。



优势
显然,申请:)

更新 -
Hibernate Doc


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



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

解决方案

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.

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 :)

UPDATE - Extract from Hibernate Doc

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天全站免登陆