Hibernate中ISessionFactory的Close方法的疑问 再者就是询问如何关闭一个ISessionFactory 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 关闭session是一个好习惯,如果每次用了不关闭的话,会消耗大量的资源,以至服务器崩溃.我建议你用一个关闭session的过滤器.这样就好了.给你代码吧.记得给分.public class CloseHibersession implements Filter { protected FilterConfig filterConfig = null; private String redirectURL = null; private List notCheckURLList = new ArrayList(); private String sessionKey = null; protected static ThreadLocal hibernateHolder = new ThreadLocal(); public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; //System.out.print("closesession"); hibernateHolder.set(HibernateSessionFactory.currentSession()); filterChain.doFilter(servletRequest, servletResponse); Session sess = (Session) hibernateHolder.get(); if (sess != null) { hibernateHolder.set(null); try { sess.close(); } catch (HibernateException ex) { throw new ServletException(ex); } } } public void destroy() { notCheckURLList.clear(); } private boolean checkRequestURIIntNotFilterList(HttpServletRequest request) { String uri = request.getServletPath() + (request.getPathInfo() == null ? "" : request.getPathInfo()); return notCheckURLList.contains(uri); } public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; redirectURL = filterConfig.getInitParameter("redirectURL"); System.out.println(redirectURL); sessionKey = filterConfig.getInitParameter("checkSessionKey");System.out.println(sessionKey); String notCheckURLListStr = filterConfig.getInitParameter("notCheckURLList");System.out.println(notCheckURLListStr); if(notCheckURLListStr != null) { StringTokenizer st = new StringTokenizer(notCheckURLListStr, ";"); notCheckURLList.clear(); while(st.hasMoreTokens()) { notCheckURLList.add(st.nextToken()); } } }} spring声明式事务遇到数据库约束操作失败的问题 遇到Hibernate比较郁闷的问题 什么是soa?哪位知道 ibatis的问题 Myeclipse+Hibernate时驱动JTDS问题和ODBC问题 web service 下载文件n慢 郁闷呢 spring声明性事务问题?急 Hibernate的问题 用JB8+Weblogic7配合发布WebService,出现发布不了的错误,请帮忙(可能比较难) JAVA可以向一个http服务器发送请求? java中如何判断某个键被按下(急急) 一个奇怪得问题,急,谢谢大家帮忙啊
public class CloseHibersession implements Filter
{
protected FilterConfig filterConfig = null;
private String redirectURL = null;
private List notCheckURLList = new ArrayList();
private String sessionKey = null;
protected static ThreadLocal hibernateHolder = new ThreadLocal();
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException
{
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
//System.out.print("closesession");
hibernateHolder.set(HibernateSessionFactory.currentSession());
filterChain.doFilter(servletRequest, servletResponse);
Session sess = (Session) hibernateHolder.get();
if (sess != null) {
hibernateHolder.set(null);
try {
sess.close();
} catch (HibernateException ex) {
throw new ServletException(ex);
}
}
}
public void destroy()
{
notCheckURLList.clear();
} private boolean checkRequestURIIntNotFilterList(HttpServletRequest request)
{
String uri = request.getServletPath() + (request.getPathInfo() == null ? "" : request.getPathInfo());
return notCheckURLList.contains(uri);
} public void init(FilterConfig filterConfig) throws ServletException
{
this.filterConfig = filterConfig;
redirectURL = filterConfig.getInitParameter("redirectURL");
System.out.println(redirectURL);
sessionKey = filterConfig.getInitParameter("checkSessionKey");
System.out.println(sessionKey);
String notCheckURLListStr = filterConfig.getInitParameter("notCheckURLList");
System.out.println(notCheckURLListStr);
if(notCheckURLListStr != null)
{
StringTokenizer st = new StringTokenizer(notCheckURLListStr, ";");
notCheckURLList.clear();
while(st.hasMoreTokens())
{
notCheckURLList.add(st.nextToken());
}
}
}
}