码迷,mamicode.com
首页 > 其他好文 > 详细

Session的获得方式

时间:2015-09-14 23:52:50      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

在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()
}

 


Session的获得方式

标签:

原文地址:http://www.cnblogs.com/freed0m/p/4808801.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!