严重: Servlet.service() for servlet [spring] in context with path [/XX] threw exception [Request

53 篇文章 0 订阅
19 篇文章 1 订阅

一、描述:最近在使用springmvc+spring+hibernate4.0进行整合开发时出现了

严重: Servlet.service() for servlet [spring] in context with path [/XX] 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


二、问题:没找到session


三、讨论:1、网上好多的人说是由于没有配置OpenSessionInViewFilter,如下:

<span style="font-size:18px;"> <filter>
		<filter-name>OpenSessionInViewFilter</filter-name>
		<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
		<init-param>
			<param-name>sessionFactoryBean</param-name>
			<param-value>sessionFactory</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>OpenSessionInViewFilter</filter-name>
		<url-pattern>/</url-pattern>
	</filter-mapping></span>
但是这种方法行不同,配置这个过滤器是为了进行hibernate的延迟加载的问题。

         2、一部分人说可以把Dao层的getCurrentSession()方法换为openSession()方法;

         3、有的人又说可以在sessionFactory里面加上 <prop key="current_session_context_class">thread</prop>,但是产生了另外一个异常XX is not valid without active transaction(这个本人没解决,欢迎大家一起讨论)

         4、还有一种解决办法是在你的SpringMVC配置文件中加入这句:<aop:aspectj-autoproxy proxy-target-class="true" />  。


四、总结:我把每种方法都尝试了一遍最终总结出两条解决方法。

   方法1把Dao层的getCurrentSession()方法换为openSession()方法,这个方法可以行的通,原因如下:

            1、 采用getCurrentSession()创建的session会绑定到当前线程中,而采用openSession()
创建的session则不会
            2、 采用getCurrentSession()创建的session在commit或rollback时会自动关闭(所以出现了没有找到session的异常),而采用openSession() 创建的session必须 手动关闭。

  方法2:在你的SpringMVC配置文件中加入这句:<aop:aspectj-autoproxy proxy-target-class="true" /> 原因如下: 在配置文件中加入<aop:aspectj-autoproxy proxy-target-class="true"/>表示使用CGLib动态代理技术织入增强,如果目标对象没有实现接口,就必须使用GCLIB,spring 会自动在动态代理和CGLIB之间转换。


这也是本人的一点总结,如果有错误不足的地方,还请大家积极指出,参与讨论,谢谢!


  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值