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);
}
}

最新文章

  1. Linux下的SVN服务器搭建
  2. css利用padding生成图标
  3. #!/usr/bin/env python与#!/usr/bin/python的区别
  4. 转载文章----初识Ildasm.exe——IL反编译的实用工具
  5. search--搜索引擎的使用笔记
  6. 十,选择cfee编辑器并学会调试程序。以及结束语。
  7. 【HTML XHTML CSS基础教程(第6版)】笔记之HTML XHTML笔记(1~6章)
  8. 关于css的兼容
  9. Microsoft Certification List
  10. 变脸不变质的桥梁模式(Bridge Pattern)
  11. 介绍一个C++奇巧淫技
  12. FAAC1.28 在海思HI3520D/HI3515A平台linux中的编译 优化
  13. 《汇编语言程序设计》——仿windows计算器
  14. Domains域
  15. FFPLAY的原理(三)
  16. Vuex 存储||获取后台接口数据
  17. shiro框架-配置
  18. Ubuntu下安装kate编辑器
  19. [转帖]看完这篇文章你还敢说你懂JVM吗?
  20. request.GetResponse()超时的解决办法

热门文章

  1. Android初体验-D3
  2. ios数据存储方式FMDB
  3. CodeIgniter框架——表单辅助函数总结
  4. 【BZOJ1923】[Sdoi2010]外星千足虫 高斯消元
  5. Nuske vs Phantom Thnook
  6. 获取html元素所在页面的坐标
  7. 九度OJ 1360:乐透之猜数游戏 (递归)
  8. 微信小程序-学习总结(1)
  9. TGraphicControl(自绘就2步,直接自绘自己,不需要调用VCL框架提供的函数重绘所有子控件,也不需要自己来提供PaintWindow函数让管理框架来调用)与TControl关键属性方法速记(Repaint要求父控件执行详细代码来重绘自己,还是直接要求Invalidate无效后Update刷新父控件,就看透明不透明这个属性,因为计算显示的区域有所不同)
  10. Ubuntu14.04下Nginx反向代理Odoo域名