在filter中关闭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
{
    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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值