首页 > 其他 > 详细

Session的获得方式

时间:2015-09-14 23:50:22      阅读:317      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!