【hibernate源码解析】之Session

Session是Hibernate持久化操作的基础。Hibernate Session 之与Hibernate,相当于JDBC Connection相对于JDBC。那么Hibernate的Session从哪里来的呢。从代码中我们可以看出来

sessionFactory.openSession();
Hibernate的Session是通过SessionFactory创建出来的。下面我们从代码中来看。

//【1】
public org.hibernate.classic.Session openSession() throws HibernateException {
		return openSession(interceptor);
	}
//【2】
public org.hibernate.classic.Session openSession(Connection connection) {
		return openSession(connection, interceptor); //prevents this session from adding things to cache
	}
从上述的两个方法中可以看出,无论你创建session时是否传递参数,程序默认的都会给你加上一个拦截器的设置,这个拦截器从哪儿来的呢?当然是从咱们的配置文件中来。从【2】看出参数是一个connection的连接。

接下来我们看一下这几个方法最终调用的方法是什么:

【最终】
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()
			);
	}

最后,openSession调用了方法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 );

		loadQueryInfluencers = new LoadQueryInfluencers( factory );

		if ( factory.getStatistics().isStatisticsEnabled() ) {
			factory.getStatisticsImplementor().openSession();
		}

		if ( log.isDebugEnabled() ) {
			log.debug( "opened session at timestamp: " + timestamp );
		}
	}

org.hibernate.stat.StatisticsImpl;<pre name="code" class="java">factory.getStatisticsImplementor().openSession();

 我们看一下这个方法是什么呢? 

<pre name="code" class="java">public synchronized void openSession() {
		sessionOpenCount++;
	}

 没有忘记对session的数量进行自增。 
















  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring和Hibernate是Java开发中非常常用的两个框架,下面简单介绍一下它们的码分析。 1. Spring码分析 Spring是一个轻量级的IoC和AOP框架,其中IoC主要是通过BeanFactory和ApplicationContext来实现的。在Spring的码中,可以看到很多设计模式的运用,如工厂模式、单例模式、策略模式、代理模式等等,这些设计模式的运用使得Spring的代码更加灵活和易于维护。 其中BeanFactory是Spring的核心接口,它提供了IoC容器的基本实现。ApplicationContext是BeanFactory的一个子类,它提供了更多的企业级特性,如AOP、事件传递等等。在Spring的码中,可以看到很多关于BeanFactory和ApplicationContext的实现,特别是在Bean的生命周期、依赖注入等方面,Spring的码展示了很多实现细节。 此外,在Spring的码中,还有很多关于AOP的实现,如代理模式、动态代理、CGLIB等等。这些实现使得Spring能够很好地支持AOP的实现,从而实现了很多企业级应用的需求。 总之,Spring的码分析可以让我们更好地了解Spring的实现细节,并且对于我们学习和使用Spring也有很大的帮助。 2. Hibernate码分析 Hibernate是一个Java持久化框架,它提供了对象/关系映射和查询语言等功能。在Hibernate码中,可以看到很多关于ORM的实现,如映射文件、Session、Transaction等等。 Hibernate的核心接口是SessionFactory和Session,它们是Hibernate的核心实现。在Hibernate码中,可以看到很多关于Session和Transaction的实现,如缓存机制、事务管理等等。此外,在Hibernate码中,还有很多关于查询语言的实现,如HQL、Criteria等等。 总之,Hibernate码分析可以让我们更好地了解Hibernate的实现细节,并且对于我们学习和使用Hibernate也有很大的帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值