/**
* @Title: OpenSessionView.java
* @Package org.util
* @Description: TODO该方法的主要作用:
* @author A18ccms A18ccms_gmail_com
* @date 2017-5-6 下午1:48:25
* @version V1.0
*/
package org.web;import java.io.IOException;import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;import org.dao.BaseHibernateDao;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;/** * * 项目名称:votedemo * 类名称:OpenSessionView * 类描述: 让session 始终保持着一个打开的状态* 创建人:Mu Xiongxiong * 创建时间:2017-5-6 下午1:48:25 * 修改人:Mu Xiongxiong * 修改时间:2017-5-6 下午1:48:25 * 修改备注: * @version * */
public class OpenSessionView extends BaseHibernateDao implements Filter {/**(非 Javadoc)* <p>Title: destroy</p>* <p>Description(描述): </p>* @see javax.servlet.Filter#destroy()*/@Overridepublic void destroy() {// TODO Auto-generated method stub}/**(非 Javadoc)* <p>Title: doFilter</p>* <p>Description(描述): </p>* @param arg0* @param arg1* @param arg2* @throws IOException* @throws ServletException* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)*/@Overridepublic void doFilter(ServletRequest arg0, ServletResponse arg1,FilterChain arg2) throws IOException, ServletException {Session session = null;Transaction tx = null;try {session = getSession();tx = session.beginTransaction();arg2.doFilter(arg0, arg1);tx.commit();} catch (Exception e) {e.printStackTrace();tx.rollback();}finally{closeSession();}}/**(非 Javadoc)* <p>Title: init</p>* <p>Description(描述): </p>* @param arg0* @throws ServletException* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)*/@Overridepublic void init(FilterConfig arg0) throws ServletException {// TODO Auto-generated method stub}}