一 懒加载优化方式
使用openSessionInView来优化懒加载。
二 openSessionInView原理图
三 过滤器代码
package com.sina.filter;
import java.io.IOException;
import java.io.PrintWriter;
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 javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.sina.util.HibernateUtil;
public class MyFilter1 extends HttpServlet implements Filter {
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
// TODO Auto-generated method stub
Session s = null;
Transaction tx = null;
try {
s = HibernateUtil.getCurrentSession();
tx = s.beginTransaction();
arg2.doFilter(arg0, arg1);
// System.out.println("ok");
tx.commit();
} catch (Exception e) {
if (tx != null) {
tx.rollback();
}
throw new RuntimeException(e.getMessage());
// TODO: handle exception
} finally {
HibernateUtil.closeCurrentSession();
}
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}