hibernate3 源码阅读 (二) Session

 

上一篇我们看了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 等。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值