Filter:
package com.dqd.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.dqd.util.HibernateUtil;
public class MyFilter extends HttpServlet implements Filter {
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
Session session = null;
Transaction tx = null;
try{
session = HibernateUtil.getCurrentSession();
tx = session.beginTransaction();
arg2.doFilter(arg0, arg1);
//提交!!!
tx.commit();
}catch(Exception e){
if(tx!=null){
tx.rollback();
}
e.printStackTrace();
}finally{
//这里的关闭不能像之前那样s!=null&&s.isopen()简单的去关闭,这里通过一个方法去关闭
//还得让ThreadLocal.set(null)
HibernateUtil.closeCurrentSession();
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
public MyFilter() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
public void init() throws ServletException {
// Put your code here
}
}
在hibernate工具类中做出相应的变化,不再使用事物和异常的捕捉(在Filter中已经实现了)
package com.dqd.util;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
final public class HibernateUtil {
private static SessionFactory sf = null;
private static ThreadLocal<Session> tl = new ThreadLocal<Session>();
private static ServiceRegistry sr= null;
private static Configuration con =null;
private HibernateUtil(){}
static{
con = new Configuration().configure();
sr = new ServiceRegistryBuilder().applySettings(con.getProperties()).buildServiceRegistry();
sf = con.buildSessionFactory(sr);
}
//获得全新的Session
public static Session openSession(){
return sf.openSession();
}
//获得和线程相关的Session
public static Session getCurrentSession(){
Session session = tl.get();
if(session==null){
session = sf.openSession();
tl.set(session);
}
return session;
}
public static List excuteQuery(String hql){
Session session = null;
//因为动作会频繁的发生所以使用事物
List list = null;
session = HibernateUtil.getCurrentSession();
list = session.createQuery(hql).list();
return list;
}
public static Object findId(Class clazz,java.io.Serializable id){
Session session = null;
//因为动作会频繁的发生所以使用事物
List list = null;
Object obj = null;
session = HibernateUtil.getCurrentSession();
obj = session.load(clazz, id);
return obj;
}
public static void closeCurrentSession(){
Session s = getCurrentSession();
if(s!=null&&s.isOpen()){
s.close();
tl.set(null);
}
}
//加强版的查询方式(防止注入漏洞)
//在这里应该考虑到并发性的问题,我们的方法是静态的但是每次我们都会信声明一个session(session不是静态),所以不会拥堵
public static List excuteQuery(String hql,String[] para){
Session s = null;
List list = null;
s = getCurrentSession();
Query query = s.createQuery(hql);
//判断是否有空串
if(para!=null&¶.length>0){
for(int i =0 ;i< para.length;++ i){
query.setString(i, para[i]);
}
}
list = query.list();
return list;
}
}