package com.itchenzn.web.filter;

import com.itchenzn.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 * 在Filter中打开一个SqlSession
 */
@WebFilter("/*")
public class OpenSqlSessionInViewFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        try{
            filterChain.doFilter(servletRequest, servletResponse);
            sqlSession.commit();
        }catch(Exception e){
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            MybatisUtils.closeSqlSession();
        }
    }

    @Override
    public void destroy() {

    }
}
