我们在使用Hibernate的lazy load来优化性能的时候,只要Session关闭后再试图访问未被载入的对象时,就会出现异常。通常使用在事务之内来访问数据是适合的,但是有时候我们需要强制载入这些数据,例如在Web视图中访问这些模型对象。
在业务层强制载入这些数据,通常不是很好的解决方案,因为不同的视图在使用业务方法的时候,需要的数据通常不一样,这样业务方法可能绑定到特定的控制器中。
在业务层上面增加一个Facade层来解决这个问题,同样也会增加一层不太必要的封装,增加了复杂性,POJO in Action一书中的例子就是这么设计的(POJO in Action感觉是本蛮不错的书),详细的可以看看这本书。
通常Open Session in View模式相对来说是个不错的解决方案。事务在服务层结束,但关联的Hibernate Session保持打开状态,直到视图生成完成为止。这样及早的释放了数据库锁和连接,并且视图中可以通过lazy load来加载。
Spring支持这种即开即用的模式,通过org.springframework.orm.hibernate.support.OpenSessionInViewFilter(可以和任何web层技术一起使用)或者OpenSessionInViewInterceptor(和Spring的Web MVC框架一起使用)。
Spring的这两种实现,他们支持两种操作方式:单一会话模式和延迟关闭模式
1、单一会话模式:
通过在请求范围的Session上来操作事务,单个Hibernate Session将用于整个HTTP请求。默认情况下是单一会话方式,它是Open Session in View比较有效的版本。请求范围内的Hibernate Session视为第一级缓存,整个请求内只载入每个持久性对象一次。主要的缺点是:所有的Session管理的对象都必须是唯一的,这样视图从HttpSession中重新attach一个对象,可能导致Hibernate重复对象的异常。
2、延迟关闭方式:
每个事务将和平常一样使用其自身的Session,但这些Session中的每一个都在事务完成后保持开启,在视图生成后关闭。这样可以通过使用新的Hibernate Session来避免重复对象的问题,所有Session都在事务完成后保持开启,在他们每个上允许lazy load。但是,如果单个持久性对象涉及到多个事务,可能导致问题。Hibernate需要一个持久化对象与一个Hibernate Session关联,而不是同时与多个。这种情况下应该使用单一会话模式。
在web.xml中配置:
<filter>
<filter-name>OpenSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate.support.OpenSessionInVewFilter</filter-class>
<!--如果使用延迟关闭方式
<init-param>
<param-name>singleSession</param-name>
<param-value>false</param-value>
</init-param>
-->
</filter>
<filter-mapping>
<filter-name>OpenSessionInView</filter-name>
<url-pattern>*.do</url-pattern><!--以.do结束的的url为例-->
</filter-mapping>
Spring Web MVC OpenSessionInViewInterceptor的配置:
<bean id="openSessionInView" class="org.springframework.orm.hibernate.support.OpenSessionInViewInterceptor">
<property name="sessionFactory">
<ref bean="sessionFactory">
</property>
<!--如果使用延迟关闭方式
<property name="singleSession">
<value>false</value>
</property>
-->
</bean>
<bean id="myUrlMapping" class="org.springframeword.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="interceptors">
<list>
<ref bean="openSessionInView">
</list>
</property>
<property name="urlMap">
<!-- url mappings-->
</property>
</bean>
分享到:
相关推荐
Open Session in View模式.PPT
struts2+hibernate3 做的小项目 使用了struts2插件实现pen session in view
在没有使用Spring提供的Open Session In View情况下,因需要在service(or Dao)层里把session关闭,所以lazy loading 为true的话,要在应用层内把关系集合都初始化,如 company.getEmployees(),否则Hibernate抛...
使用Spring提供的Open Session In View而引起Write operations are not allowed in read-only mode (FlushMode.NEVER) 错误解决
SSH整合概述 应用IoC进行整合 应用AOP进行整合 Open Session In View模式
为了练手培训,给大家准备的 Open Session In View 的简单例子,纯代码,大家可以参考,其中主要说了六部分内容: 1.通过接口编程 2.通过spring注入dao到 action 3.通过 open session in view filter 支持 延迟加载...
它演示了MvC 、 SoC 、 IoC 、 DAO 、 Service layer和Open Session in View模式以及其他 J2EE 最佳实践。 该模板可以轻松扩展为功能齐全的基于 Wicket 的 Web 应用程序。 用法 该模板使用 maven 并在 Tomcat7 上...
View detailed session, code lab, and speaker information, including speaker bios, photos, and Google+ profiles +1 sessions right from the app Participate in public #io12 conversations on Google+ Guide...
Open Session; Close Session; Secure Session; Manage Cookie; Block User by Cookie; Create Google Chart; Curl Get Contents; Fetch Wiki Page; Fetch Flickr Stream; Get Yahoo! Answers; Search Yahoo!; Get ...
You can open multiple files in the Open dialogue box, by holding down the Shift or Ctrl key down as you select each file. You can press ESC to return from Full Screen mode to normal mode.
In this tutorial, we'll cover some of the basics of Unicode-encoded text and Unicode files, and how to view and manipulate it in UltraEdit. Search and delete lines found UEStudio and UltraEdit provide...
* Describe Window now also shows the view comments in the header * Export Tables would change nls_date_format in single session mode and dual session mode * Auto Replace now supports Undo to continue...
- fixed bug with URLs in Open Document Text and Open Document Spreadsheet exports - fixed format string in XLS OLE export - fixed format string in XLS BIFF8 export - fixed output of the check boxes ...
Encodes the specified URL by including the session ID in it, or, if encoding is not needed, returns the URL unchanged. -------------------------------------------------------------------------------...
* Multi-select files in project-view (when "double-click to open" is configured in Environment Settings) * Resource files are treated as ordinary files now * Updates in "Project Options/Files" code * ...
更新的内容包含: Version 10.8 (2018-07-07) Bugfix: fixed the error message which occured when running the graphical package manager...引用地址:http://www.open-open.com/lib/view/open1437704662490.html
It is stored in CBCGPGridCtrl::m_PrintParams::m_pageInfo member and in CPrintInfo::m_lpUserData member of the CPrintInfo object used while printing at the current print session. Added an option to ...
File encoding of open files is now stored in the session Build Systems may define a cancel command using the "cancel" key Syntax: Added clear_scopes directive, to give more control over the generated ...
Triggers can now be created in the context of a table or view Folders added for Primary, Unique and Foreign key constraints for views Search Bar enhancements A new Search in files option allows you ...
Added a statusbar to the hexview in memoryview Pointerscan for value scans now add the results to the overflow queue Opening a file and changing bytes do not change them to the file anymore (you need ...