这是一个过滤器,用于确保关闭session资源,也用于获得session资源:HibernateFilter.getSession()
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.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateFilter implements Filter {
private static ThreadLocal hibernaThreadLocal = new ThreadLocal();
private static SessionFactory factory;
public void destroy() {
}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
try {
//加载页面之前
filterChain.doFilter(servletRequest, servletResponse);
}finally {
//页面加载完hibernate使用的对象后关闭资源
Session session = (Session)hibernaThreadLocal.get();
if(session != null) {
if(session.isOpen()) {
session.close();
}
hibernaThreadLocal.remove();
}
}
}
public void init(FilterConfig filterConfig) throws ServletException {
try {
Configuration cfg = new Configuration().configure();
factory = cfg.buildSessionFactory();
}catch(Exception e) {
e.printStackTrace();
throw new ServletException(e);
}
}
public static Session getSession() {
Session session = (Session)hibernaThreadLocal.get();
if(session == null) {
session = factory.openSession();
hibernaThreadLocal.set(session);
}
return session;
}
}
分享到:
相关推荐
介绍Spring+hibernate opensessioninview问题
为了练手培训,给大家准备的 Open Session In View 的简单例子,纯代码,大家可以参考,其中主要说了六部分内容: 1.通过接口编程 2.通过spring注入dao到 action 3.通过 open session in view filter 支持 延迟加载...
在没有使用Spring提供的Open Session In View情况下,因需要在service(or Dao)层里把session关闭,所以lazy loading 为true的话,要在应用层内把关系集合都初始化,如 company.getEmployees(),否则Hibernate抛...
Spring提供的CharacterEncoding和OpenSessionInView功能
OpenSessionInView项目整合jar包
SSH项目整合示例【OpenSessionInView】所用到的jar包 包含Struts + Hibernate + Spring所有jar及其依赖的jar
最新版SSH项目整合 实现了OpenSessionInView。 hibernate5.2.3+struts2.5.2+spring4.3.3
NULL 博文链接:https://8366.iteye.com/blog/472141
2,但在加载时,如果Session已经关掉了就会抛LazyInitializationException异常 二,集成 Spring 与 Struts2.1.8.1 1,在web.xml配置监听器(Spring Reference 15.2 Common configuration) <!-- 集成Spring -...
NULL 博文链接:https://likenice.iteye.com/blog/290162
spring2.5学习PPT 传智博客 01_全面阐释Spring及其各项功能 ... 30.Spring提供的CharacterEncoding和OpenSessionInView功能 31.使用Spring集成JPA 32.Struts+Spring+JPA集成 33.使用Spring集成Struts2 34.所有源码
和Spring中OpenSessionInView由于org.springframework.web.struts.ContextLoaderPlugIn中保存同一个对象的名不同导致openSessionInView失效 稍微修改后在struts-config.xml中使用MyContextLoaderPlugIn.jar包中...
NULL 博文链接:https://yanzhenwei.iteye.com/blog/1701164
SSH项目整合示例【OpenSessionInView】源码
使用 AOC 拦截器而不是过滤器进行 OpenSessionInView 处理 列出一个国家的城市和添加城市的可能性(bean 验证和自动检索 lat 和 lng 坐标) 安全 安全的 REST Web 服务 使用 CSRF 令牌 角度父状态的问题 有用的链接 ...
Spring 的配置文件:..........................................................................................................7 1.3.4 在配置中配置类:........................................................