Differences between session vs session factory - Hibernate?

后端 未结 7 1629
予麋鹿
予麋鹿 2021-01-31 10:35

Do we have any other differences other than the below? Also please validate whether the below are correct

  1. SessionFactory objects are one per applicati
7条回答
  •  无人及你
    2021-01-31 11:17

    Sessionfactory:

    1. It is one instance per datasource/database.
    2. It is thread-safe.
    3. It is a heavyweight object, because it maintains data sources, mappings, hibernate configuration information etc.
    4. Sessionfactory will create and manage the sessions.
    5. If you have say, 4 datasources/databases, then you must create 4 session factory instances.
    6. sessionfactory is an immutable object and it will be created as a singleton while the server initializes itself.

    Session:

    1. It is one instance per client/thread/one transaction.
    2. It is not thread-safe.
    3. It is lightweight.
    4. sessions will be opened using sessionfactory.openSession() and some database operations will be done finally session will be closed using session.close().

提交回复
热议问题