zoukankan      html  css  js  c++  java
  • 解决no-session延迟加载问题

    解决no-session延迟加载问题

    说明:dao层使用Hibernate从数据库查询数据信息返回给web层,在web层打印信息报no-Session错误,

    产生原因

    • 关联对象默认都是采用延迟加载
    • 事务都是在业务层(service)开启和提交的
    • 所以调用完业务层后, 事务已经提交,session已经关闭了
    • 由于关联的对象延迟加载 当web层使用关联对象时, session已经不在了, 所以会报no-session异常

    解决办法

    • 1.不使用延迟加载
      • 效率太低
    • 2.使用spring提供的解决方案OpenInViewFilter
      提供了一个过滤器
      在web视图层提供session的打开和关闭
                    <!--配置延迟加载-->
    				    <filter>
    				        <filter-name>OpenSessionInViewFilter</filter-name>
    				        <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
    				    </filter>
    				
    				    <filter-mapping>
    				        <filter-name>OpenSessionInViewFilter</filter-name>
    				        <url-pattern>*.action</url-pattern>
    				    </filter-mapping>
    
  • 相关阅读:
    memcache 应用场景
    如何写接口文档(登录)
    PHP常见错误级别及错误码
    ex33 while 循环
    ex32 循环和列表
    ex31--作出决定
    ex29-30 if,elif and else.
    ex28 布尔表达式练习
    ex25 更多更多的实践
    ex21 函数可以返回某些东西
  • 原文地址:https://www.cnblogs.com/zhou-shi-yuan-ISO8859-1/p/10300297.html
Copyright © 2011-2022 走看看