Why use only one SessionFactory Object per application?

前端 未结 4 1128
醉酒成梦
醉酒成梦 2020-12-04 10:45

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

4条回答
  •  [愿得一人]
    2020-12-04 11:06

    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.

提交回复
热议问题