Hibernate Core Reference Manual学习笔记——Chapter 2. Architecture

Hibernate Core Reference Manual

Chapter 2. Architecture


Table of Contents
2.1. Overview
2.1.1. Minimal architecture
2.1.2. Comprehensive architecture
2.1.3. Basic APIs
2.2. JMX Integration
2.3. Contextual sessions


2.1. Overview
站在很高的层次看Hibernate,它是下面这个样子:


 
由于Hibernate架构十分的灵活,所以我们不可能提供所有可能的运行时架构。这里只介绍介绍两种特殊情况:
2.1.1. Minimal architecture
最简架构:应用程序自己管理它的JDBC连接,并把这些连接提供给Hibernate。同时,应用程序自己管理transaction。这种方式使用了Hibernate API的一个最小子集。


 
2.1.2. Comprehensive architecture
"comprehensive"架构使应用程序从底层的JDBC和JTA API抽象出来,并让Hibernate来管理这些细节。



2.1.3. Basic APIs

SessionFactory (org.hibernate.SessionFactory)

A thread-safe, immutable不可变的 cache of compiled mappings for a single database. A factory for org.hibernate.Session instances. A client oforg.hibernate.connection.ConnectionProvider. Optionally maintains a second level cache of data that is reusable between transactions at a process or cluster 集群level.

Session (org.hibernate.Session)

A single-threaded, short-lived object representing a conversation between the application and the persistent store. Wraps a JDBC java.sql.Connection. Factory fororg.hibernate.Transaction. Maintains a first level cache of persistent the application's persistent objects and collections; this cache is used when navigating the object graph or looking up objects by identifier.

Persistent objects and collections

Short-lived, single threaded objects containing persistent state and business function. These can be ordinary JavaBeans/POJOs. They are associated with exactly one org.hibernate.Session. Once the org.hibernate.Session is closed, they will be detached and free to use in any application layer (for example, directly as data transfer objects to and from presentation). Chapter 11, Working with objects discusses transient, persistent and detached object states.

Transient and detached objects and collections

Instances of persistent classes that are not currently associated with a org.hibernate.Session. They may have been instantiated by the application and not yet persisted, or they may have been instantiated by a closed org.hibernate.Session. Chapter 11, Working with objects discusses transient, persistent and detached object states.

Transaction (org.hibernate.Transaction)

(Optional) A single-threaded, short-lived object used by the application to specify atomic units of work. It abstracts the application from the underlying JDBC, JTA or CORBA transaction. A org.hibernate.Session might span several org.hibernate.Transactions in some cases. However, transaction demarcation, either using the underlying API or org.hibernate.Transaction, is never optional.

ConnectionProvider (org.hibernate.connection.ConnectionProvider)

(Optional) A factory for, and pool of, JDBC connections. It abstracts the application from underlying javax.sql.DataSource or java.sql.DriverManager. It is not exposed to application, but it can be extended and/or implemented by the developer.

TransactionFactory (org.hibernate.TransactionFactory)

(Optional) A factory for org.hibernate.Transaction instances. It is not exposed to the application, but it can be extended and/or implemented by the developer.

Extension Interfaces

Hibernate offers a range of optional extension interfaces you can implement to customize the behavior of your persistence layer. See the API documentation for details.



2.2. JMX Integration
JMX is the J2EE standard for the management of Java components. Hibernate can be managed via a JMX standard service. AN MBean implementation is provided in the distribution: org.hibernate.jmx.HibernateService.


2.3. Contextual sessions
大多数使用Hibernate的应用程序都需要某种形式的"contextual" session,在这个给定的context的scope中,session都是有效地。然而,通常情况下,应用程序对 “由什么组成context”的定义是不同的;由于对current的理解不同,导致不同的context定义不同的scope。


使用Hibernate 3.0(及以前)的应用程序通常会利用ThreadLocal-based contextual sessions和辅助类HibernateUtil,或者是利用第三方的框架,例如Spring和Pico,他们可以提供proxy/interception-based contextual sessions.


从Hibernate 3.0.1开始,Hibernate添加了 SessionFactory.getCurrentSession()方法。起初,这个方法假定使用JTA transactions,而JTA transaction定义当前session的scope和context。考虑到很多独立的JTA TransactionManager实现都很成熟,在绝大多数情况下,应用程序应该使用JTA transaction management。不论应用程序是否部署到J2EE容器中去。基于此,the JTA-based contextual sessions are all you need to use.


但是到了Hibernate 3.1,SessionFactory.getCurrentSession()底层的处理变成了可插拔的。为了实现这一目标,一个新的接口org.hibernate.context.spi.CurrentSessionContext和一个新的配置参数 hibernate.current_session_context_class被添加进来。这就允许你以可插拔的方式配置当前session的scope和context。

查看 org.hibernate.context.spi.CurrentSessionContext接口的Javadocs.可以看到它仅定义了一个方法currentSession()。它的实现类负责追踪current contextual session。同时Hibernate提供了对该接口的三种实现:

前两种实现提供了"one session - one database transaction"编程模型,也被用为session-per-request。一个Session的开始和结束由一个 database transaction来定义。If you use programmatic transaction demarcation in plain JSE without JTA,建议你使用Hibernate Transaction API来隐藏底层实现。如果你是用JTA,你可以利用JTA接口来界定transactions。如果你在支持CMT的EJB容器中运行,transaction的边界可以声明性的定义,而且在你的代码中不需要任何界定transaction和session的操作。

配置参数hibernate.current_session_context_class用来定义使用org.hibernate.context.spi.CurrentSessionContext的哪一个实现类。为了向后兼容,如果这个参数没有设置,但是设置了org.hibernate.transaction.TransactionManagerLookup的值,Hibernate就会使用org.hibernate.context.internal.JTASessionContext。通常hibernate.current_session_context_class的值就是你要使用的实现类的名字。对于上面提到的三个实现,它们各自有对应的短名字"jta", "thread", 和"managed".

猜你喜欢

转载自soloplayer.iteye.com/blog/1753884