此内容没有您所选择的语言版本。
11.2. Session and Transaction Scopes
11.2.1. About Session and Transaction Scopes 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
A
SessionFactory
is an expensive-to-create, threadsafe object, intended to be shared by all application threads. It is created once, usually on application startup, from a Configuration
instance.
A
Session
is an inexpensive, non-threadsafe object that should be used once and then discarded for: a single request, a conversation or a single unit of work. A Session
will not obtain a JDBC Connection
, or a Datasource
, unless it is needed. It will not consume any resources until used.
In order to reduce lock contention in the database, a database transaction has to be as short as possible. Long database transactions will prevent your application from scaling to a highly concurrent load. It is not recommended that you hold a database transaction open during user think time until the unit of work is complete.
What is the scope of a unit of work? Can a single Hibernate
Session
span several database transactions, or is this a one-to-one relationship of scopes? When should you open and close a Session
and how do you demarcate the database transaction boundaries? These questions are addressed in the following sections.