<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所創(chuàng)建,sessionFactory是線程安全的,你可以讓多個線程同時存取SessionFactory,而不會有資源共用的問題,然而session不是設(shè)計為線程安全的,所以讓多個線程共用一個session,將發(fā)生資料共用而發(fā)生混亂的問題.下面是一個標準類.
    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
    {
         
    //創(chuàng)建線程局部變量 tLocalsess 
        public static final ThreadLocal tLocalsess = new ThreadLocal();
       
    //創(chuàng)建線程局部變量 tLocaltx
    public static final ThreadLocal tLocaltx = new ThreadLocal();
          
    //取得session
        public static Session currentSession(){
             
    //從線程變量tLocalsess中,取得當前session
    Session session = (Session) tLocalsess.get();
    //判斷session是否為空,如果為空,將創(chuàng)建一個session,并付給線程變量tLocalsess
        try{
    if (session == null){
    session 
    = openSession();
    tLocalsess.set(session);
    }

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

    return session;
    }

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

             
    //從線程變量tLocalsess中,取得當前session
    Session session = (Session) tLocalsess.get();
             
    //設(shè)置線程變量tLocalsess為空
    tLocalsess.set(null);
    try{
                
    //關(guān)閉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中創(chuàng)建一個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技術(shù)
    主站蜘蛛池模板: 亚洲AV无码一区二区三区久久精品| 成人毛片免费在线观看| 立即播放免费毛片一级| 亚洲美女中文字幕| 亚洲国产成人片在线观看 | 亚洲av无码一区二区三区乱子伦 | 色偷偷亚洲第一综合网| 亚洲国产精品综合久久网各| 亚洲成a人片在线观看日本| 亚洲人成影院在线无码观看| 午夜一区二区免费视频| 免费看污成人午夜网站| 精品一区二区三区免费毛片爱| 成在线人免费无码高潮喷水| 一级做a爰黑人又硬又粗免费看51社区国产精品视 | 亚洲一区二区三区四区视频| 久久亚洲AV成人无码软件| 国产亚洲AV无码AV男人的天堂| 亚洲成av人片不卡无码久久| 国产三级电影免费观看| 看全色黄大色大片免费久久| 毛片视频免费观看| 中文字幕人成无码免费视频| 美丽的姑娘免费观看在线播放 | 亚洲视频在线观看视频| 婷婷久久久亚洲欧洲日产国码AV| 亚洲午夜国产精品无码老牛影视| 日韩精品亚洲aⅴ在线影院| 久久精品国产亚洲AV不卡| 亚洲综合色在线观看亚洲| 亚洲AV无码成人精品区大在线| 国产yw855.c免费视频| 免费一级毛片正在播放| 国产精品黄页在线播放免费| 国产成人在线观看免费网站| 国产片免费在线观看| 国产中文字幕免费| 国内精品久久久久久久亚洲| 久久亚洲国产精品五月天婷| 亚洲精品自在在线观看| 亚洲色欲色欲综合网站|