上一篇我们看了Configuration 初始化
Configuration cfg = new Configuration();
这篇看下和Session相关的
SessionFactory factory = cfg.buildSessionFactory();
从Configuration 取得一个 SessionFactory ,
public SessionFactory buildSessionFactory() throws HibernateException {
log.debug( "Preparing to build session factory with filters : " + filterDefinitions );
secondPassCompile();
validate();
Environment.verifyProperties( properties );
Properties copy = new Properties();
copy.putAll( properties );
PropertiesHelper.resolvePlaceHolders( copy );
Settings settings = buildSettings( copy );
return new SessionFactoryImpl(
this,
mapping,
settings,
getInitializedEventListeners(),
sessionFactoryObserver
);
}
里边要先得到 Settings ,然后用它作为参数,构造SessionFactoryImpl,
Settings 类用来存放hibernate 运行过程中一些信息,配置等,如后边将提到的ConectionProvider。
这个类以后再看,继续看Session,
Session s = factory.openSession();
看一下 SessionFactory 的 openSession 方法,
private SessionImpl openSession(
Connection connection,
boolean autoClose,
long timestamp,
Interceptor sessionLocalInterceptor
) {
return new SessionImpl (
connection,
this,
autoClose,
timestamp,
sessionLocalInterceptor == null ? interceptor : sessionLocalInterceptor,
settings.getDefaultEntityMode(),
settings.isFlushBeforeCompletionEnabled(),
settings.isAutoCloseSessionEnabled(),
settings.getConnectionReleaseMode()
);
}
第一个参数 需要一个Connection, 默认情况下是null,
第二个参数 需要一个SessionFactory,SessionFactoryImpl 把自己 (this) 作为参数,
这样一个 SessionImpl 就可以随时回头访问 SessionFactoryImpl 里的内容了,
hibernate中,这种用法比较多,通过this 关键字,把一个个类串联起来。
我们知道,操作数据库是围绕着 jdbc 进行的,hibernate也一样,
在 sessionImpl 中,有一个比较重要的成员类,JdbcContext,在 SessionImpl 初始化时,他也一同初始化,
SessionImpl(
final Connection connection,
final SessionFactoryImpl factory,
final boolean autoclose,
final long timestamp,
final Interceptor interceptor,
final EntityMode entityMode,
final boolean flushBeforeCompletionEnabled,
final boolean autoCloseSessionEnabled,
final ConnectionReleaseMode connectionReleaseMode) {
super( factory );
this.rootSession = null;
this.timestamp = timestamp;
this.entityMode = entityMode;
this.interceptor = interceptor;
this.listeners = factory.getEventListeners();
this.actionQueue = new ActionQueue( this );
this.persistenceContext = new StatefulPersistenceContext( this );
this.flushBeforeCompletionEnabled = flushBeforeCompletionEnabled;
this.autoCloseSessionEnabled = autoCloseSessionEnabled;
this.connectionReleaseMode = connectionReleaseMode;
this.jdbcContext = new JDBCContext( this, connection, interceptor );
if ( factory.getStatistics().isStatisticsEnabled() ) {
factory.getStatisticsImplementor().openSession();
}
if ( log.isDebugEnabled() ) {
log.debug( "opened session at timestamp: " + timestamp );
}
}
JdbcContext 的初始化在 SessionImpl 的构造函数中,也就是JdbcContext随着SessonImpl 一同被初始化,
this.jdbcContext = new JDBCContext( this, connection, interceptor );
第一个参数是 this,还是老样子,把自己的引用做参数,
第二个参数是 connection,前边我们说过,默认情况下,这个connection 是null,
第三个参数是 interceptor,这个参数 jdbcContext 碰都没碰,直接传个了ConnectionManager,
我们再看 ConnectionManager 的初始化,
ConnectionManager 是hibernate中取数据库连接 (Connection) 的类,
包在jdbcContext 中, 做为jdbcContext 的一个成员,
jdbcContext 要想操作数据库,就要得到数据库的 Connection,
想得到数据库的Connection,就得找他,
public JDBCContext(Context owner, Connection connection, Interceptor interceptor) {
this.owner = owner;
this.connectionManager = new ConnectionManager(
owner.getFactory(),
this,
owner.getConnectionReleaseMode(),
connection,
interceptor
);
final boolean registerSynchronization = owner.isAutoCloseSessionEnabled()
|| owner.isFlushBeforeCompletionEnabled()
|| owner.getConnectionReleaseMode() == ConnectionReleaseMode.AFTER_TRANSACTION;
if ( registerSynchronization ) {
registerSynchronizationIfPossible();
}
}
JdbcContext随着SessonImpl 一同初始化,而 Connectionmanager 则随着 JdbcContext 一同初始化,
this.connectionManager = new ConnectionManager(
owner.getFactory(),
this,
owner.getConnectionReleaseMode(),
connection,
interceptor
);
第一个参数,owner 实际就是 SessionImpl,因为SessionImpl 之前把自身作为this传了进来,
第二个参数, 又是 this。
以上两篇,是 hibernate 初始化的简要过程,包括得Configuration,SessionFactory,Session,JdbcContext,ConnectionManager 等。