1.问题描述:对于根据id查询时,在dao通过load方式查询对象时,加载页面会报 noSession异常。

 严重: Servlet.service() for servlet [springDispatcherServlet] in context with path [/Xxxx] threw exception 

[Request processing failed; nested exception is org.hibernate.HibernateException: No Session found for current thread] 

with root cause
org.hibernate.HibernateException: No Session found for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:106)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:988)
at com.vincent.videosys.dao.BaseDao.getSession(BaseDao.java:17)
at com.vincent.videosys.dao.UserDao.usernameExist(UserDao.java:29)
at com.vincent.videosys.service.UserService.usernameExistService(UserService.java:19)
at com.vincent.videosys.controller.home.UserController.usernameExist(UserController.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:214)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle

2.问题分析:当使用hibernate框架操作数据库的时候,如果做查询的话会有立即加载(get)和延迟加载(load)的区别,延迟加载表示,当你查询某个数据(假设是对象)的时候,hibernate不会立马发送sql语句,而是当我们调用这个对象的属性的时候,也就是真正使用查询出来的数据的时候才会发送sql语句去一级缓存(即session,这里的session和域对象session没有半毛钱关系)中获取,但是正常这个session的开启核关闭是在service层执行的,但是我们真正使用查询的对象的数据时,是在web层,但是这个时候session已经关闭,就会报no-session异常。

          noSession分析图(右键"查看图像"查看原图)

3.问题解决思路

  方案一:让session的关闭时间要在web层使用完之后。 但是web层已经是最后一层了,怎么办?还有比web更后的东西哦,就是过滤器, 所以在web.xml中配置开启和关闭session的过滤器即可 ,但是要配在struts的过滤器之前,否则无效。

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

 

          添加过滤器解决noSession分析图

   使用load方法的解决方案 : 就是把原CustomerService层的绑定的session对象 提取配置到前面的 过滤器中了。

   方案二:

   load改用get立即加载方式查询对象。

package cn.xdf.dao.impl;
@Repository
public class CustomerDaoImpl implements CustomerDao { @Autowired
private HibernateTemplate hibernateTemplate; public void save(Customer customer) {
hibernateTemplate.save(customer);
} public List<Customer> findAll() {
return (List<Customer>) hibernateTemplate.find("from Customer");
} public void delete(Customer customer) {
hibernateTemplate.delete(customer);
}
//根据id立即加载
public Customer get(Long custId) {
return hibernateTemplate.get(Customer.class, custId);
}
//根据id延迟加载-->用该方法会有问题(页面报错:noSession)
public Customer load(Long custId) {
return hibernateTemplate.load(Customer.class, custId);
} public void update(Customer customer) {
hibernateTemplate.update(customer);
} public List<Customer> findByCriteria(DetachedCriteria dc) {
return (List<Customer>) hibernateTemplate.findByCriteria(dc);
}
}

解决noSession问题的更多相关文章

  1. 解决no-session延迟加载问题

    解决no-session延迟加载问题 说明:dao层使用Hibernate从数据库查询数据信息返回给web层,在web层打印信息报no-Session错误, 产生原因 关联对象默认都是采用延迟加载 事 ...

  2. NoSession问题

    第一种原因: no Session 错误  dao层中get方法换成了load方法,或者其他原因引起. 原因分析: 真正用到代理对象的时候,代理对象没有值,并且session的生命周期已经走完了. 解 ...

  3. Hibernate 中 load() 方法导致的 noSession 异常

    之所以要写这个,是因为最近碰到了一个延迟加载的 load() 导致出现 noSession 的异常. 下面第三种方式解决这个问题需要用到一个本地线程的对象,也就是 ThreadLocal 类,之前写过 ...

  4. 整合Spring+Hibernate+Struts2的时候发现json数据一直无法传到页面,提示no-Session

    执行了ajax,页面没有任何反应 怀疑json没有值,想查看json中的内容,使用了ObjectMapper: ObjectMapper om=new ObjectMapper(); System.o ...

  5. Hibernate(四)——缓存策略+lazy

    Hibernate作为和数据库数据打交道的框架,自然会设计到操作数据的效率问题,而对于一些频繁操作的数据,缓存策略就是提高其性能一种重要手段,而Hibernate框架是支持缓存的,而且支持一级和二级两 ...

  6. on-session问题

    .D:\0kecheng\bos\bosv2.0_chapter03.无条件查询. 方法1.@JSON(serialize=false)是注解排除不需要加载的实体类上,找到它的get方法,解决no-s ...

  7. 第三天的 No session 问题

    1.1 No session(理解) 初始化快递员对象中 定区集合 Web层转Courier对象为json串时候,对象中有fixedareas集合属性,jpa集合属性加载策略延迟加载.在action中 ...

  8. 项目一:第三天 收派标准添加 收派标准分页查询(基于datagrid实现) 收派标准修改快递员添加 快递员列表查询

    1.收派标准添加 n jQuery easyUI window使用 n jQuery easyUI form表单校验 n 收派标准添加页面调整—url params n 服务端实现—三层 2.jQue ...

  9. SSH框架整合截图总结(一)

    分页相关属性 --------------------------------------------------------------- 分页思路表单提交(只需传递当前页的值) ->acti ...

随机推荐

  1. 解决erlang R17无法识别中文问题

    erlang更新到R17已有一段时间了.公司项目打算从旧版的erlang迁移到R17,却不料有不少的困扰,当中一个问题是中文问题. 这个问题非常easy重现:新建一个文件t.erl.保存为utf-8无 ...

  2. 43、android:screenOrientation

    android:screenOrientationThe orientation of the activity's display on the device. The value can be a ...

  3. 【BZOJ3325】[Scoi2013]密码 Manacher

    [BZOJ3325][Scoi2013]密码 Description Fish是一条生活在海里的鱼.有一天他很无聊,就到处去寻宝.他找到了位于海底深处的宫殿,但是一扇带有密码锁的大门却阻止了他的前进. ...

  4. A - 士兵队列训练问题

    A - 士兵队列训练问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit ...

  5. EasyNVR摄像机无插件直播按需RTSP拉流播放流程详解

    1.背景需求 有许多客户现场,有许多设备但是不需要一直向设备端取视频流,因为在用户不观看的情况下,还在获取视频资源,一方面大大的浪费了网络带宽资源,一方面对设备服务器要求也较高,用户成本就要提高,这是 ...

  6. SharePoint服务器端对象模型 之 使用CAML进行数据查询

    (一)概述 在SharePoint的开发应用中,查询是非常常用的一种手段,根据某些筛选.排序条件,获得某个列表或者某一些列表中相应的列表条目的集合. 除去列表上的查询之外,在SharePoint中还大 ...

  7. 扩展类 HOW TO EXTEND CLASSES TO MAKE NEW CLASSES IN PYTHON

    How to Extend Classes to Make New Classes in Python - dummies https://www.dummies.com/programming/py ...

  8. 洛谷 P4109 [HEOI2015]定价

    洛谷 这是今天的考试题,我来发一波- 看L和R的范围,显然不能一个一个加,这样会有很多重复情况,会超时,只要看该数有多少个后导0,就把L加上10的多少次方. 很容易可以想到价格后面尽可能多0, 在此基 ...

  9. vloatile总结与synchronized对比

    原文地址:https://www.cnblogs.com/xiaoxian1369/p/5411877.html 1.要使volatile变量提供理想的线程安全,必须同时满足以下两个条件:1).对变量 ...

  10. Quartz使用总结(转发:http://www.cnblogs.com/drift-ice/p/3817269.html)

    Quartz可以用来做什么? Quartz是一个任务调度框架.比如你遇到这样的问题 想每月25号,信用卡自动还款 想每年4月1日自己给当年暗恋女神发一封匿名贺卡 想每隔1小时,备份一下自己的爱情动作片 ...