用了OpenSessionInViewFilter之后,程序开发确实少了很多问题。一个直接的优点就是,不用再去关心lazy-loading的问题,因为在一个Request的处理周期内,Session都是Open的。 但是,"Great Charge means great responsibility",更大的方便也就意味着更大的责任。此Filter运用时也要非常小心。 从OpenSessionInViewFilter的文档中,我们可以看到下面一段话:
引用
WARNING: Applying this filter to existing logic can cause issues that have not appeared before, through the use of a single Hibernate Session for the processing of an entire request. In particular, the reassociation of persistent objects with a Hibernate Session has to occur at the very beginning of request processing, to avoid clashes with already loaded instances of the same objects.
Alternatively, turn this filter into deferred close mode, by specifying "singleSession"="false": It will not use a single session per request then, but rather let each data access operation or transaction use its own session (like without Open Session in View). Each of those sessions will be registered for deferred close, though, actually processed at request completion.
A single session per request allows for most efficient first-level caching, but can cause side effects, for example on saveOrUpdate or if continuing after a rolled-back transaction. The deferred close strategy is as safe as no Open Session in View in that respect, while still allowing for lazy loading in views (but not providing a first-level cache for the entire request).
Looks up the SessionFactory in Spring's root web application context. Supports a "sessionFactoryBeanName" filter init-param in web.xml; the default bean name is "sessionFactory". Looks up the SessionFactory on each request, to avoid initialization order issues (when using ContextLoaderServlet, the root application context will get initialized after this filter).
NOTE: This filter will by default not flush the Hibernate Session, as it assumes to be used in combination with service layer transactions that care for the flushing, or HibernateAccessors with flushMode FLUSH_EAGER. If you want this filter to flush after completed request processing, override closeSession and invoke flush on the Session before closing it. Additionally, you will also need to override getSession() to return a Session in a flush mode other than the default FlushMode.NEVER. Note that getSession and closeSession will just be invoked in single session mode!
在Hibernate的论坛里面,我也看到了类似的话:
引用
// Quote An alternative solution that you don't need to implement yourself is to use the Spring Franmework's OpenSessionInViewFilter class. You simply define a servlet filter mapping for the filter and it takes care of opening a Session when a request comes in and closing it when the response is generated. In fact, it will even bind the Session to the current thread and Spring provides a static SessionFactoryUtils.getSession() method that intelligently returns the bound Session or a new one (optionally). We use this pattern in our Struts application and it works like a charm. You can reliably get the one Session even if you use jsp:include and you can pass around your Hibernate POJOs without worrying about lazy initialization exceptions anywhere. However, be aware that having a single Session open for an entire request/response cycle can result in some unexpected behavior that did not happen before. We encountered this ourselves, but dealt with it by using the JUnit setUp() and tearDown() methods to mimic the OpenSessionInViewFilter behavior in our unit tests and solving the quirks from there.
Single Session虽然可以最大限度的使用Session中的一级缓存,然而,却可能带来很多的副作用。
分享到:
相关推荐
OpenSessionInViewFilter是Spring框架中一个非常重要的组件,主要用于解决数据访问层(DAO)与视图层(View)之间的事务管理问题。在Web应用中,由于HTTP请求的无状态性,一次请求通常由多个Servlet过滤器、控制器和...
OpenSessionInViewFilter个人学习总结
SSH是Java Web开发中的一个流行框架组合,由Struts、Hibernate和Spring三个组件构成。这个框架集成提供了模型-视图-控制器(MVC)架构,数据持久化,以及依赖注入和事务管理等功能,大大简化了企业级应用的开发工作...
- 在高并发场景下,需要注意OpenSessionInViewFilter可能会导致的性能问题,因为它使得Session的生命周期变长,增加了Session占用资源的时间。 - 如果项目中同时使用了其他Session管理机制,需要确保它们之间不会...
在探讨如何通过`OpenSessionInViewFilter`来有效管理Hibernate中的Session生命周期之前,我们需要先理解Session及其生命周期的基本概念。 #### Session与生命周期 在Hibernate框架中,`Session`是执行数据库操作的...
Struts2、Spring和Hibernate是Java开发中三个非常重要的框架,它们共同构成了经典的MVC(Model-View-Controller)架构的实现。这个压缩包“struts2.0.8+spring2.0+hibernate3.2”包含了这三个框架的特定版本,这将...
该模式的核心在于通过Spring提供的`OpenSessionInViewFilter`过滤器,在视图渲染过程中保持Hibernate Session的打开状态,从而简化了事务管理,并避免了一些常见的懒加载异常。 #### 一、OpenSessionInViewFilter...
在处理Web应用时,Spring提供了一些关键特性,如`CharacterEncodingFilter`和`OpenSessionInViewFilter`,它们对于解决特定问题至关重要。 首先,让我们深入了解一下`CharacterEncodingFilter`。在Web应用中,字符...
OpenSessionInViewFilter解决Web应用程序的问题
为了解决这个问题,我们可以使用 Spring 提供的一个支持类OpenSessionInViewFilter,这个类可以在发起一个页面请求时打开 Hibernate 的 Session,并保持这个 Session 的生命周期,直到这个请求结束。这样可以确保 ...
### Spring监听器与过滤器详解...- **Spring Web环境下的监听器和过滤器**:在Spring MVC环境中,除了上述提到的OpenSessionInViewFilter和CharacterEncodingFilter,还有多种其他类型的过滤器和监听器可以使用,例如`...
2. **OpenSessionInViewFilter**:通过Web容器的过滤器(filter)来实现。 #### 异常原因 当使用Open Session In View模式时,如果Session的Flush Mode被设置为NEVER,并且尝试执行写操作(如更新或删除),就会触发...
在`web.xml`中配置Spring监听器以初始化Spring容器,并添加`OpenSessionInViewFilter`以解决懒加载问题: ```xml <listener-class>org.springframework.web.context.ContextLoaderListener <filter-name>...
在 OpenSessionInViewFilter 中,需要配置 Hibernate 的 Session 管理,以便实现 Hibernate 的 Session 的打开和关闭。 9. web.xml 配置 在 web.xml 文件中,需要配置 Servlet 监听器、上下文变量、Filter 等,...
Spring MVC Hibernate Demo Hibernate 配置 数据库实体必须设置以下注解 @Entity ... <filter>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter <param>flushMode</param-nam
这个案例提供了一个基础的登录功能,同时展示了S2SH集成的各个方面,包括OpenSessionInViewFilter的使用、声明式事务管理以及三层架构的设计。 首先,让我们从Struts2开始。Struts2作为控制层,负责处理HTTP请求,...
Spring框架还提供了一系列的过滤器来处理Web应用中的请求,例如OpenSessionInViewFilter用于解决Hibernate懒加载问题。 - **OpenSessionInViewFilter**:此过滤器在请求处理开始时打开一个Hibernate会话,在请求...
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter ``` - **`OpenSessionInViewFilter`**:开启Hibernate的Session,在视图渲染完成后关闭Session,确保每个HTTP请求都在同一个...
在使用Hibernate进行对象持久化时,经常遇到的一个问题是关于懒加载(lazy loading)的处理。懒加载是一种优化技术,允许在真正需要某个关联对象的数据时才加载它们,而不是一开始就加载所有相关联的数据。这种策略...
以上就是关于 Spring 学习笔记中的核心知识点详解,包括面向接口编程、IOC/DI、AOP、Spring 框架介绍及其在实际开发中的应用等方面的内容。通过深入理解这些知识点,可以帮助开发者更好地掌握 Spring 框架的核心原理...