问题描述
为什么每个应用程序只使用一个SessionFactory对象?每个应用程序使用单个会话工厂对象的优点是什么?
会话工厂对象将使用单身设计模式。 SessionFactory的实例是线程安全的,并且通常在整个应用程序中共享。由于这些对象包含连接信息,hibernate配置信息和映射文件以及位置路径,因此这些对象的权重很重。因此,创建多个实例会使我们的应用程序重量。但会话对象不是线程安全的。简而言之,SessionFactory对象是每个应用程序一个,Session对象每个客户端一个。
因此,它将是每个DataSource一个SessionFactory。您的应用程序可能有多个DataSource,因此您可能在该实例中拥有多个SessionFactory。但您不希望在应用程序中多次创建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
这篇关于为什么每个应用程序只使用一个SessionFactory对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!