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. MySQL的order by子句

    1.语法:select 字段列表 from 表名 [where 子句][group by 子句][having 子句][order by 子句]; 注解: 1.默认是从第一条记录开始升序, 2.des ...

  2. 关于java类加载器的一些概念

    顾名思义,类加载器(class loader)用来加载 Java 类到 Java 虚拟机中.一般来说,Java 虚拟机使用 Java 类的方式如下:Java 源程序(.java 文件)在经过 Java ...

  3. HTML5的兴起与4G网络的出现,能否够终止移动端的持续下滑走向

    HTML5的兴起与4G网络的出现,能否够终止移动端的持续下滑走向. 每当大家谈起互联网的未来的时候,多半谈及的是云.大数据.SAAS.仿佛要将一切摒弃.而当谈起移动互联网的时候.却坚持觉得NATIVE ...

  4. 自然语言处理(NLP)资源

    1.HMM学习最佳范例全文文档,百度网盘链接: http://pan.baidu.com/s/1pJoMA2B 密码: f7az 2.无约束最优化全文文档 -by @朱鉴 ,百度网盘链接:链接:htt ...

  5. POJ2417 Discrete Logging【BSGS】

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5577   Accepted: 2494 ...

  6. 《从零开始学Swift》学习笔记(Day 47)——final关键字

    原创文章,欢迎转载.转载请注明:关东升的博客 在类的定义中使用final关键字声明类.属性.方法和下标.final声明的类不能被继承,final声明的属性.方法和下标不能被重写. 下面看一个示例: f ...

  7. 获取html元素所在页面的坐标

    function findPosition(oElement) { var x2 = 0; var y2 = 0; var width = oElement.offsetWidth; var heig ...

  8. AJAX 异步传数组时候,后台接收不到!

    呵呵,那么问题又来了! 这是为啥呢!! var ids= []; $.ajax({ url: 'xxxx.do', data: { ids: ids}, dataType: "json&qu ...

  9. 问题:Unable to find a 'userdata.img' file for ABI armeabi to copy into the AVD folder.

    创建AVD时,发现创建不成功,报错“Unable to find a 'userdata.img' file for ABIarmeabi to copy into the AVD folder.” ...

  10. 004-RIP、OSPF【路由选择协议】

    常见的路由选择协议有:RIP协议.OSPF协议. 1.RIP协议 路由信息协议(英语:Routing Information Protocol,缩写:RIP)是一种内部网关协议(IGP),为最早出现的 ...