freed0m

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

 


分类:

技术点:

相关文章:

  • 2021-05-29
  • 2021-09-17
  • 2021-11-21
  • 2022-12-23
  • 2022-12-23
  • 2021-07-20
  • 2021-05-25
猜你喜欢
  • 2022-12-23
  • 2021-11-01
  • 2021-08-04
  • 2022-12-23
  • 2022-12-23
  • 2021-08-10
  • 2022-12-23
相关资源
相似解决方案