`

Open Session in View

阅读更多
    我们在使用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>
分享到:
评论
3 楼 fuliang 2009-02-17  
taga 写道

可能导致Hibernate重复对象的异常??这句话没看明白

就是NonUniqueObjectException,Session中存在两个对象具有不同的ObjectID,但具有相同的ID(对用于数据库表的主键)
在一个会话的多次请求可能导致对象的复制,这样就回出现两个对象具有不同的ObjectID,但具有相同的ID(对用于数据库表的主键)的情况。这是不允许的,因为这样会处于不一致的状态,如果flush不知道去save那个对象到数据库中。
2 楼 taga 2009-02-17  
可能导致Hibernate重复对象的异常??
这句话没看明白
1 楼 zaiyibo 2008-07-29  
底色弄成黑色很不好看的!

相关推荐

    Open Session in View模式.PPT

    Open Session in View模式.PPT

    struts2+hibernate3 open session in view

    struts2+hibernate3 做的小项目 使用了struts2插件实现pen session in view

    Open_Session_In_View详解.doc

    在没有使用Spring提供的Open Session In View情况下,因需要在service(or Dao)层里把session关闭,所以lazy loading 为true的话,要在应用层内把关系集合都初始化,如 company.getEmployees(),否则Hibernate抛...

    使用Spring引起的错误

    使用Spring提供的Open Session In View而引起Write operations are not allowed in read-only mode (FlushMode.NEVER) 错误解决

    SSH整合 struts+hibernate+spring

    SSH整合概述 应用IoC进行整合 应用AOP进行整合 Open Session In View模式

    Struts Spring Hibernate 整合 OpenSessionInView 例子

    为了练手培训,给大家准备的 Open Session In View 的简单例子,纯代码,大家可以参考,其中主要说了六部分内容: 1.通过接口编程 2.通过spring注入dao到 action 3.通过 open session in view filter 支持 延迟加载...

    spring-jpa-wicket-bootstrap:使用 Spring、JPA、Hibernate、Wicket 和 Bootstrap 的 J2EE Web 模板。 在 Tomcat 和 Postgres DB 上测试

    它演示了MvC 、 SoC 、 IoC 、 DAO 、 Service layer和Open Session in View模式以及其他 J2EE 最佳实践。 该模板可以轻松扩展为功能齐全的基于 Wicket 的 Web 应用程序。 用法 该模板使用 maven 并在 Tomcat7 上...

    Android google io 2012 opensource已通过编译无错误

    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...

    plug-In PHP- 100 Power Solutions

    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 ...

    JCreatorV4

    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.

    UE(官方下载)

    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...

    PLSQL Developer 8.0.3.1510 中文注册版下载

    * 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...

    FastReport.v4.15 for.Delphi.BCB.Full.Source企业版含ClientServer中文修正版支持D4-XE5

    - 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 ...

    servlet2.4doc

    Encodes the specified URL by including the session ID in it, or, if encoding is not needed, returns the URL unchanged. -------------------------------------------------------------------------------...

    Bloodshed Dev-C++

    * 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 * ...

    MobaXterm 10.8最新professional版专业版

    更新的内容包含: 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

    BCGControlBarPro.v12.00

    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 ...

    Sublime Text Build 3124 x64 Setup.exe

    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 ...

    PLSQL.Developer v11.0.0.1762 主程序+ v10中文包+keygen

    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 ...

    CE中文版-启点CE过NP中文.exe

    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 ...

Global site tag (gtag.js) - Google Analytics