该帖已经被评为精华帖
|
|
---|---|
作者 | 正文 |
发表时间:2005-03-16
/* * Created on 2004-11-25 * */ package com.bupticet.append.hibernate.util; import net.sf.hibernate.*; import net.sf.hibernate.cfg.*; /** * @author LJ-silver */ public class HibernateUtil { private static final SessionFactory sessionFactory; static { try { //Create the SessionFactory sessionFactory = new Configuration();.configure(); .buildSessionFactory();; } catch (HibernateException ex); { throw new RuntimeException("Configuration problem: " + ex.getMessage();, ex);; } } public static final ThreadLocal session = new ThreadLocal();; public static Session currentSession(); throws HibernateException { Session s = (Session); session.get();; //Open a new Session, if this Thread has none yet if (s == null); { s = sessionFactory.openSession();; session.set(s);; } return s; } public static void closeSession(); throws HibernateException { Session s = (Session); session.get();; session.set(null);; if (s != null); s.close();; } } package com.bupticet.test; import java.io.*; import javax.servlet.*; import net.sf.hibernate.HibernateException; import net.sf.hibernate.Session; import com.bupticet.append.hibernate.util.HibernateUtil; public class HibernateFilter implements Filter { public HibernateFilter(); { } /** * destroy * * @todo Implement this javax.servlet.Filter method */ public void destroy(); { } /** * doFilter * * @param servletRequest ServletRequest * @param servletResponse ServletResponse * @param filterChain FilterChain * @throws IOException * @throws ServletException * @todo Implement this javax.servlet.Filter method */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain); throws IOException, ServletException { try{ request.setAttribute("HibernateSession",HibernateUtil.currentSession(););; chain.doFilter(request, response);; }catch(HibernateException he);{ he.printStackTrace();; }finally{ try{ HibernateUtil.closeSession();; }catch(HibernateException he);{ he.printStackTrace();; } } } /** * init * * @param filterConfig FilterConfig * @throws ServletException * @todo Implement this javax.servlet.Filter method */ public void init(FilterConfig filterConfig); throws ServletException { } } 在jsp或servlet获取hibernate session的方法: Session s = (Session);request.getAttribute("HibernateSession");; 可以将sesion作为参数传给dao。 |
|
返回顶楼 | |
发表时间:2005-03-16
过滤器中这么写:
package com.bupticet.test; import java.io.*; import javax.servlet.*; import net.sf.hibernate.HibernateException; import net.sf.hibernate.Session; import com.bupticet.append.hibernate.util.HibernateUtil; public class HibernateFilter implements Filter { public HibernateFilter(); { } /** * destroy * * @todo Implement this javax.servlet.Filter method */ public void destroy(); { } /** * doFilter * * @param servletRequest ServletRequest * @param servletResponse ServletResponse * @param filterChain FilterChain * @throws IOException * @throws ServletException * @todo Implement this javax.servlet.Filter method */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain); throws IOException, ServletException { try{ chain.doFilter(request, response);; }catch(Exception he);{ he.printStackTrace();; }finally{ try{ HibernateUtil.closeSession();; }catch(HibernateException he);{ he.printStackTrace();; } } } /** * init * * @param filterConfig FilterConfig * @throws ServletException * @todo Implement this javax.servlet.Filter method */ public void init(FilterConfig filterConfig); throws ServletException { } } 在dao中直接用HibernateUtil.currentSession()创建Session,不用去管怎么关闭session. 请各位大哥看看是不是这样? |
|
返回顶楼 | |