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

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

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

    posts - 60,comments - 71,trackbacks - 0

            session是hibernate運做的核心,是有SessionFactory所創建,sessionFactory是線程安全的,你可以讓多個線程同時存取SessionFactory,而不會有資源共用的問題,然而session不是設計為線程安全的,所以讓多個線程共用一個session,將發生資料共用而發生混亂的問題.下面是一個標準類.
    import java.io.Serializable;
    import net.sf.hibernate.HibernateException;
    import net.sf.hibernate.Session;
    import net.sf.hibernate.SessionFactory;
    import net.sf.hibernate.Transaction;
    public class HibernateSessionUtil implements Serializable
    {
         
    //創建線程局部變量 tLocalsess 
        public static final ThreadLocal tLocalsess = new ThreadLocal();
       
    //創建線程局部變量 tLocaltx
    public static final ThreadLocal tLocaltx = new ThreadLocal();
          
    //取得session
        public static Session currentSession(){
             
    //從線程變量tLocalsess中,取得當前session
    Session session = (Session) tLocalsess.get();
    //判斷session是否為空,如果為空,將創建一個session,并付給線程變量tLocalsess
        try{
    if (session == null){
    session 
    = openSession();
    tLocalsess.set(session);
    }

    }
    catch (HibernateException e){
    throw new InfrastructureException(e);
    }

    return session;
    }

    //關閉當前session
        public static void closeSession(){

             
    //從線程變量tLocalsess中,取得當前session
    Session session = (Session) tLocalsess.get();
             
    //設置線程變量tLocalsess為空
    tLocalsess.set(null);
    try{
                
    //關閉session
    if (session != null && session.isOpen()){
    session.close();
    }

    }
    catch (HibernateException e){
    throw new InfrastructureException(e);
    }

    }

    //事物處理
        public static void beginTransaction(){
          
    //從線程變量tLocaltx中取得事物管理對象Transaction
            Transaction tx = (Transaction) tLocaltx.get();
    try{
                
    //如果為空就從session中創建一個tx
    if (tx == null){
    tx 
    = currentSession().beginTransaction();
    tLocaltx.set(tx);
    }

    }
    catch (HibernateException e){
    throw new InfrastructureException(e);
    }

    }

    //提交事物
        public static void commitTransaction(){
          
    //取得事物
    Transaction tx = (Transaction) tLocaltx.get();
    try{
                
    //如果不為空就提交
    if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack())
    tx.commit();
    tLocaltx.set(
    null);
    }
    catch (HibernateException e){
    throw new InfrastructureException(e);
    }

    }

        
    //事物回滾    
        public static void rollbackTransaction(){
             
    //取得tx事物
    Transaction tx = (Transaction) tLocaltx.get();
    try{
                
    //將變量清空
    tLocaltx.set(null);
    if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()){
                    
    //事物回滾
    tx.rollback();
    }

    }
    catch (HibernateException e){
    throw new InfrastructureException(e);
    }

    }


       
    //取得session
    private static Session openSession() throws HibernateException{
    return getSessionFactory().openSession();
    }


       
    //取得sessionFactory
    private static SessionFactory getSessionFactory() throws HibernateException{
    return SingletonSessionFactory.getInstance();
    }

    }

    filter的代碼:
    public class HibernateSessionCloser implements Filter{
    protected FilterConfig filterConfig = null;
    public void init(FilterConfig filterConfig)throws ServletException{
    this.filterConfig = filterConfig;
    }

    public void destroy(){
    this.filterConfig = null;
    }

    public void doFilter(ServletRequest request, ServletResponse response,
    FilterChain chain)
    throws IOException, ServletException {
    try{
    chain.doFilter(request, response);
    }

    finally{
    try{
    HibernateSessionUtil.commitTransaction();
    }
    catch (InfrastructureException e){
    HibernateSessionUtil.rollbackTransaction();
    }
    finally{
    HibernateSessionUtil.closeSession();
    }

    }

    }

    }


    然后在web.xml配置filter就可以使用了.
    posted on 2008-05-18 20:36 henry1451 閱讀(630) 評論(0)  編輯  收藏 所屬分類: Hibernate技術
    主站蜘蛛池模板: 亚洲福利秒拍一区二区| 久久国产免费一区二区三区| 亚洲AV无码国产精品麻豆天美| 免费高清在线爱做视频| aⅴ免费在线观看| 免费一级不卡毛片| 一级毛片免费播放试看60分钟| 亚洲欧洲另类春色校园网站| 亚洲综合婷婷久久| 亚洲国产无套无码av电影| 亚洲精品456播放| 国产免费变态视频网址网站| 久久WWW免费人成人片| 最刺激黄a大片免费网站| 免费看少妇高潮成人片| 国产精品免费看久久久香蕉| 黄色免费网站在线看| 自拍偷自拍亚洲精品播放| 亚洲精品成a人在线观看☆| 亚洲成a人片在线看| 亚洲国产精品成人久久久| 亚洲成人免费在线观看| 亚洲综合激情六月婷婷在线观看| 久久精品亚洲视频| 亚洲AV永久无码精品一百度影院| 91麻豆精品国产自产在线观看亚洲 | 久久免费看少妇高潮V片特黄| 99久久免费国产特黄| 国产精品美女久久久免费| 永久免费观看黄网站| 无码日韩人妻AV一区免费l | 在线日韩av永久免费观看| 国产大片91精品免费观看男同 | 亚洲国产精品无码久久九九大片| 亚洲自偷自偷在线成人网站传媒 | 日韩免费a级在线观看| 色视频色露露永久免费观看 | 一级毛片高清免费播放| 爱情岛论坛免费视频| 又硬又粗又长又爽免费看| eeuss草民免费|