<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技術
    主站蜘蛛池模板: 噼里啪啦电影在线观看免费高清 | 亚洲一级片在线观看| 亚洲精品视频在线播放| 久久狠狠爱亚洲综合影院| 亚洲大码熟女在线观看| 国产亚洲成在线播放va| 999zyz**站免费毛片| 99蜜桃在线观看免费视频网站| 国产va精品免费观看| 免费很黄很色裸乳在线观看| 自拍偷自拍亚洲精品被多人伦好爽| 久久精品国产亚洲| 激情综合亚洲色婷婷五月APP| 亚洲精品成人片在线观看精品字幕| 亚洲AV无码第一区二区三区| 亚洲电影日韩精品 | 在线A亚洲老鸭窝天堂| 亚洲免费视频在线观看| 国产精品高清视亚洲一区二区| 欧洲美女大片免费播放器视频 | 特级精品毛片免费观看| 成年女人视频网站免费m| 亚洲午夜日韩高清一区| 国产成人免费永久播放视频平台| 亚洲国产成人精品无码久久久久久综合 | 亚洲国产日韩视频观看| 色网站在线免费观看| 免费的全黄一级录像带| 成年美女黄网站18禁免费| 在线日韩日本国产亚洲| 亚洲伦理一二三四| 一级日本高清视频免费观看| 永久免费在线观看视频| 亚洲免费日韩无码系列| 亚洲伊人久久精品| 亚洲第一成年人网站| 思思久久99热免费精品6| 精品视频免费在线| 91精品免费在线观看| 国产成人亚洲影院在线观看| 色偷偷女男人的天堂亚洲网|