<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 閱讀(635) 評論(0)  編輯  收藏 所屬分類: Hibernate技術
    主站蜘蛛池模板: 亚洲免费在线视频播放| 粉色视频成年免费人15次| 伊人免费在线观看| 亚洲国产免费综合| 国产午夜亚洲不卡| 2022年亚洲午夜一区二区福利| 中文字幕不卡高清免费| 国产aⅴ无码专区亚洲av麻豆| 国产亚洲精品国产| 亚洲天然素人无码专区| 我想看一级毛片免费的| 亚洲日韩精品无码专区加勒比| 成人无遮挡毛片免费看| 亚洲a∨无码精品色午夜| 免费国产不卡午夜福在线| 亚洲国产精品久久久久婷婷老年| 久久99精品视免费看| 无码专区一va亚洲v专区在线| 亚洲另类激情综合偷自拍| 免费女人高潮流视频在线观看| 亚洲国产精品婷婷久久| 中文字幕无码成人免费视频| 国产亚洲真人做受在线观看| 免费无码又爽又刺激高潮视频 | 色吊丝性永久免费看码| 久9这里精品免费视频| 亚洲精品美女视频| 免费无码又爽又刺激聊天APP| 亚洲AV无码国产剧情| 亚洲综合色在线观看亚洲| 午夜免费啪视频在线观看| 亚洲香蕉久久一区二区三区四区| 午夜老司机免费视频| 久久夜色精品国产亚洲AV动态图| 美女黄网站人色视频免费| 日韩在线视频免费看| caoporn国产精品免费| 亚洲成a人在线看天堂无码| 精品国产一区二区三区免费| 亚洲精品国产成人影院| 99精品一区二区免费视频|