`
jaychang
  • 浏览: 719263 次
  • 性别: Icon_minigender_1
  • 来自: 嘉兴
社区版块
存档分类
最新评论

HibernateUtil,filter中管理session

阅读更多
利用Thread-Specific Storage撰寫一個 HibernateUtil
HibernateSessionUtil.java

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
{
    public static final ThreadLocal tLocalsess = new ThreadLocal();

    public static final ThreadLocal tLocaltx = new ThreadLocal();

    /*
     * getting the thread-safe session for using
     */
    public static Session currentSession(){
        Session session = (Session) tLocalsess.get();

        //open a new one, if none can be found.
        try{
            if (session == null){
                session = openSession();
                tLocalsess.set(session);
            }
        }catch (HibernateException e){
            throw new InfrastructureException(e);
        }
        return session;
    }

    /*
     * closing the thread-safe session
     */
    public static void closeSession(){

        Session session = (Session) tLocalsess.get();
        tLocalsess.set(null);
        try{
            if (session != null && session.isOpen()){
                session.close();
            }

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

    /*
     * begin the transaction
     */
    public static void beginTransaction(){
        Transaction tx = (Transaction) tLocaltx.get();
        try{
            if (tx == null){
                tx = currentSession().beginTransaction();
                tLocaltx.set(tx);
            }
        }catch (HibernateException e){
            throw new InfrastructureException(e);
        }
    }

    /*
     * close the transaction
     */
    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);
        }
    }

    /*
     * for rollbacking
     */
    public static void rollbackTransaction(){
        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);
        }
    }

    private static Session openSession() throws HibernateException{
        return getSessionFactory().openSession();
    }

    private static SessionFactory getSessionFactory() throws HibernateException{
        return SingletonSessionFactory.getInstance();
    }
}

 filter中的程式碼如下
HibernateSessionCloser.java

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();
            }   
        }

    }
}

然後在操作資料庫之前加上

HibernateSessionUtil.beginTransaction();
HibernateSessionUtil.currentSession();//取得Session

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics