zoukankan      html  css  js  c++  java
  • Hibernate——openSession和getCurrentSession区别

    openSession和getCurrentSession区别:

    深入讨论:
    在SessionFactory启动的时候,Hibernate会根据配置创建相应的CurrentSessionContext
    在getCurrentSession()被调用的时候,实际上执行的方法是:CurrentSessionContext.currentSession()
    在currentSession()执行时,如果当前Session为空,currentSession会调用SessionFactory的openSession


    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    * private static SessionFactory sessionFactory = null;
    * /*使用线程局部模式*/
    * private static ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
    * static{
    * sessionFactory = new Configuration().configure().buildSessionFactory();
    * }
    * /**
    * * 获取和线程关联的session
    * * @return
    * */
    * public static Session getCurrentSession(){
    * Session session = threadLocal.get();
    * /*判断是否得到*/
    * if(session==null || !session.isOpen()){
    * session = sessionFactory.openSession();
    * //把session对象设置到threadLocal,相当于该session和线程绑定
    * threadLocal.set(session);
    * }
    * return session;
    * }
    * /**
    * * 获取全新的session
    * * @return
    * */
    * public static Session openSession(){
    * return sessionFactory.openSession();
    * }
    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

  • 相关阅读:
    JQ 鼠标滑过按钮改变背景图片
    TreeView 用法(有代码)
    MVC框架 IE浏览时IIS配置
    table 边框 不显示好td内为空串时,边框不显示
    DIV 内滚动条 样式的写法
    用 Grid 数据绑定
    架设支持Silverlight的Web服务器
    c# sql like @参数
    IE8 的margintop兼容问题
    弱符号与强符号
  • 原文地址:https://www.cnblogs.com/qintangtao/p/2744808.html
Copyright © 2011-2022 走看看