<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-57  評論-202  文章-17  trackbacks-0
          這一節是非常實用的一節,我在閱讀此書的時候,一直在迷惑,究竟應該怎樣管理Session呢?因為Session的管理是如此重要,類似于以前寫程序對JDBC Connection的管理。看完此節后,終于找到了方法。
          在各種Session管理方案中,ThreadLocal模式得到了大量使用。ThreadLocal是Java中一種較為特殊的線程綁定機制。通過ThreadLocal存取的數據,總是與當前線程相關,也就是說,JVM為每個運行的線程,綁定了私有的本定實例存取空間,從而為多線程環境經常出現的并發訪問問題提供了一種隔離機制。
          下面是Hibernate官方提供的一個ThreadLocal工具:


    import net.sf.hibernate.
    *;
    import net.sf.hibernate.cfg.
    *;
    import org.apache.log4j.Logger;

    /**
     * <p>Title: </p>
     *
     * <p>Description: Session的管理類</p>
     *
     * <p>Copyright: Copyright (c) 2005</p>
     *
     * <p>Company: </p>
     *
     * @author George Hill
     * @version 1.0
     
    */


    public class HibernateUtil {

      
    private static final Logger log = Logger.getLogger(HibernateUtil.class);

      
    private static final SessionFactory sessionFactory;

      
    /**
       * 初始化Hibernate配置
       
    */

      
    static {
        
    try {
          
    // Create the SessionFactory
          sessionFactory = new Configuration().configure().buildSessionFactory();
        }
     catch (Throwable ex) {
          log.error(
    "Initial SessionFactory creation failed.", ex);
          
    throw new ExceptionInInitializerError(ex);
        }

      }


      
    public static final ThreadLocal session = new ThreadLocal();

      
    /**
       * 根據當前線程獲取相應的Session
       * @return Session
       * @throws HibernateException
       
    */

      
    public static Session currentSession() throws HibernateException {
        Session s 
    = (Session) session.get();
        
    // Open a new Session, if this Thread has none yet
        if (s == null{
          s 
    = sessionFactory.openSession();
          session.
    set(s);
        }

        
    return s;
      }


      
    /**
       * 返回Session給相應的線程
       * @throws HibernateException
       
    */

      
    public static void closeSession() throws HibernateException {
        Session s 
    = (Session) session.get();
        session.
    set(null);
        
    if (s != null)
          s.close();
      }


    }


          針對WEB程序,還可以利用Servlet2.3的Filter機制,輕松實現線程生命周期內的Session管理。下面是一個通過Filter進行Session管理的典型案例:

    public class PersistenceFilter implements Filter {
      
    protected static ThreadLocal hibernateHolder = new ThreadLocal();

      
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        hibernateHolder.
    set(getSession());
        
    try {
          
          chain.doFilter(request, response);
          
        }
     finally {
          Session session 
    = (Session) hibernateHolder.get();
          
    if (session != null{
            hibernateHolder.
    set(null);
            
    try {
              session.close();
            }
     catch (HibernateException ex) {
              
    throw new ServletException(ex);
            }

          }

        }

      }

      
    }
    posted on 2005-07-29 19:43 小米 閱讀(3938) 評論(1)  編輯  收藏 所屬分類: Hibernate

    評論:
    # re: 《深入淺出Hibernate》讀書筆記(9)——Session管理 2005-11-06 15:07 | jiniboy
    這都是你總結的嗎   回復  更多評論
      
    主站蜘蛛池模板: 伊人久久精品亚洲午夜| 国产乱子影视频上线免费观看| 亚洲色偷偷偷鲁综合| 9久热这里只有精品免费| 亚洲自偷自偷图片| 最新国产乱人伦偷精品免费网站 | 两个人日本WWW免费版| 亚洲无码高清在线观看| 久久久久久噜噜精品免费直播| 亚洲AV无码专区日韩| 巨胸喷奶水www永久免费| 亚洲色大成网站WWW久久九九 | 一个人免费观看www视频| 久久亚洲国产精品五月天婷| 国产三级在线免费观看| 亚洲av色福利天堂| 很黄很黄的网站免费的| tom影院亚洲国产一区二区| 成人免费777777| 一级毛片大全免费播放下载| 亚洲中文字幕无码一区二区三区 | 日韩精品无码免费一区二区三区| 久久青青草原亚洲av无码app| 青青青免费国产在线视频小草| 亚洲日韩久久综合中文字幕| 一区国严二区亚洲三区| 中文字幕免费在线播放| 亚洲午夜电影在线观看| 国产精品成人四虎免费视频| 中文在线日本免费永久18近| 亚洲欧洲日韩国产| 免费人成视频在线观看不卡| 两个人看的www高清免费观看| 亚洲国产综合精品| 亚洲成AⅤ人影院在线观看| 一区二区三区观看免费中文视频在线播放 | 亚洲国产夜色在线观看| 亚洲国产av一区二区三区| 一区二区免费视频| 麻豆一区二区三区蜜桃免费| 亚洲精品视频在线|