liuzhen6835 2015-10-03 09:19 采纳率: 51.2%
浏览 1466

在web.xml中添加了OpenSessionInViewFilter依然有延迟加载的错误

web.xml

<?xml version="1.0" encoding="UTF-8"?>
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

OpenSessionInViewFilter
org.springframework.orm.hibernate.support.OpenSessionInViewFilter


OpenSessionInViewFilter
/*


contextConfigLocation
classpath:applicationContext.xml


org.springframework.web.context.ContextLoaderListener


index.jsp


struts2

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter



struts2
/*

struts.xml


<!-- 交给spring来配置 -->


DAO层:
@Repository
public class CommonDaoImpl implements ICommonDao
{
private HibernateTemplate hibernateTemplate;
@Resource
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
@Override
public List queryAll(String hql)
{
return this.hibernateTemplate.find(hql);
}
Biz层:
@Service
public class CrmBizImpl implements ICrmBiz
{
private ICommonDao sysRightDao;
@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
public List queryAllRight()
{
return sysRightDao.queryAll("Form SysRight");
}
@Resource
public void setSysRightDao(ICommonDao sysRightDao)
{
this.sysRightDao = sysRightDao;
}

}
Test:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/applicationContext.xml")
public class JTest extends AbstractJUnit4SpringContextTests
{
@Resource
private CrmAction crmAction;
@Test
public void test()
{
........
}
在我用spring test+junit进行测试的时候是 no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize

  • 写回答

1条回答 默认 最新

  • 大河行脚 2015-10-03 10:29
    关注

    这个应该不是你的配置问题而是你的代码问题;而且用OpenSessionInViewFilter也不是最好的选择;当你页面量很大而且网站访问量很大时,一打开渲染页面
    就让session开着,这对数据库造成很大的压力的;通常的做法是在你的事务类中强行的初始化页面,也就是在这个类里写一句废话让代理立即执行就行了;

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看