解决noSession问题
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问题的更多相关文章
- 解决no-session延迟加载问题
解决no-session延迟加载问题 说明:dao层使用Hibernate从数据库查询数据信息返回给web层,在web层打印信息报no-Session错误, 产生原因 关联对象默认都是采用延迟加载 事 ...
- NoSession问题
第一种原因: no Session 错误 dao层中get方法换成了load方法,或者其他原因引起. 原因分析: 真正用到代理对象的时候,代理对象没有值,并且session的生命周期已经走完了. 解 ...
- Hibernate 中 load() 方法导致的 noSession 异常
之所以要写这个,是因为最近碰到了一个延迟加载的 load() 导致出现 noSession 的异常. 下面第三种方式解决这个问题需要用到一个本地线程的对象,也就是 ThreadLocal 类,之前写过 ...
- 整合Spring+Hibernate+Struts2的时候发现json数据一直无法传到页面,提示no-Session
执行了ajax,页面没有任何反应 怀疑json没有值,想查看json中的内容,使用了ObjectMapper: ObjectMapper om=new ObjectMapper(); System.o ...
- Hibernate(四)——缓存策略+lazy
Hibernate作为和数据库数据打交道的框架,自然会设计到操作数据的效率问题,而对于一些频繁操作的数据,缓存策略就是提高其性能一种重要手段,而Hibernate框架是支持缓存的,而且支持一级和二级两 ...
- on-session问题
.D:\0kecheng\bos\bosv2.0_chapter03.无条件查询. 方法1.@JSON(serialize=false)是注解排除不需要加载的实体类上,找到它的get方法,解决no-s ...
- 第三天的 No session 问题
1.1 No session(理解) 初始化快递员对象中 定区集合 Web层转Courier对象为json串时候,对象中有fixedareas集合属性,jpa集合属性加载策略延迟加载.在action中 ...
- 项目一:第三天 收派标准添加 收派标准分页查询(基于datagrid实现) 收派标准修改快递员添加 快递员列表查询
1.收派标准添加 n jQuery easyUI window使用 n jQuery easyUI form表单校验 n 收派标准添加页面调整—url params n 服务端实现—三层 2.jQue ...
- SSH框架整合截图总结(一)
分页相关属性 --------------------------------------------------------------- 分页思路表单提交(只需传递当前页的值) ->acti ...
随机推荐
- ios 集成阿里百川的坑-【SDK初始化-iOS】读取身份图片AppKey失败
最简易方法调用淘宝app: 引用文件 #import <AlibcTradeSDK/AlibcTradeSDK.h> AlibcWebViewController* view = [[Al ...
- java php 3des实现
php.java.android.ios通用的3des方法:http://blog.csdn.net/zcjwsrf/article/details/47659137 PHP使用3DES算法加密解密字 ...
- excel添加复选框和去掉复选框
添加复选框 我测试的excel版本是最新版2016,所有版本都是找开发者工具里面包含很多工具呢,大家可以慢慢测试 excel的右上角 点击文件-->选项-->自定义功能区-->添加开 ...
- Android开发:《Gradle Recipes for Android》阅读笔记(翻译)2.3——用Eclipse ADT导出App
问题: 想在一个已经存在的Eclipse ADT的项目中使用Gradle 解决方案: Eclipse ADT插件可以帮助生成Gradle文件 讨论: Eclipse的ADT插件是在2013年推出Gra ...
- SQL Server 还原错误“restore database正在异常终止 错误 3154”
今天在还原数据库时,先建立相同名字的数据库,然后在该数据库上右键还原数据库.遇到了这样的一个错误: “备份集中的数据库备份与现有的 'RM_DB' 数据库不同. RESTORE DATABASE 正在 ...
- CODEVS 必做题:3149、2821、1531、3369、1230
3149 爱改名的小融 2 时间限制: 2 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description Wikioi上有个人叫小融,他喜 ...
- UESTC 491 Tricks in Bits
Tricks in Bits Time Limit: 1000MS Memory Limit: 65535KB 64bit IO Format: %lld & %llu Submit ...
- view {display:block}
view {display:block}
- standard pbr(二)
下一步看像素着色器代码 half4 fragBase (VertexOutputForwardBase i) : SV_Target { return fragForwardBaseInternal( ...
- Python3.6全栈开发实例[020]
20.判断一个数是否是水仙花数, 水仙花数是一个三位数, 三位数的每一位的三次方的和还等于这个数. 那这个数就是一个水仙花数, 例如: 153 = 1**3 + 5**3 + 3**3 num = i ...