Session的获得方式

在hibernate.cfg.xml中添加这个属性,来开启currentSession的使用
<property name= "hibernate.current_session_context_class">thread</property>

    SessionFactory sessionFactory = configuration.buildSessionFactory();

而buildSessionFactory(){
//每次都会重新而new一个sessionFactoryImpl,工厂模式!所以是线程安全
        return new SessionFactoryImpl(
                this,
                mapping,
                settings,
                getInitializedEventListeners(),
                sessionFactoryObserver
            );
}

sessionFactory.getCurrentSession(){
  通过ThreadLocal<Map<SessionFactory,session>> 绑定,让获得session的线程唯一!
    1.要使用currentSession,要在cfg。xml重配置
    2.必须开启事务
    3.sessio.commit之后自动关闭session,不用手动session.close()
}

 


posted on 2015-09-14 22:59  freedom's_blog  阅读(250)  评论(0编辑  收藏  举报

导航