org.hibernate.HibernateException: No Session found for current thread

spring3+hibernate4出错解决方法如下:在web.xml添加如下配置即可

<!-- openSessionInView配置 -->

<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>*.action</url-pattern>

</filter-mapping>

 

today is 2014/4/13,,今天又遇到这个问题了,还得记录一下,通过上面的方式解决显然并不是十分给力啊。。。。,,,,研究了好长时间终于又学到了点知识,,nice...

之前我配置的事务为如下方式:

<!-- 拦截器方式配置事物 -->
    <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="upd*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="modify*" propagation="REQUIRED" />
            <tx:method name="edit*" propagation="REQUIRED" />
            <tx:method name="del*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="remove*" propagation="REQUIRED" />
            <tx:method name="repair" propagation="REQUIRED" />
            <tx:method name="persistence*" propagation="REQUIRED" />

            <!--请注意这里 propagation="SUPPORTS" -->
            <tx:method name="get*" propagation="SUPPORTS" />
            <tx:method name="find*" propagation="SUPPORTS" />
            <tx:method name="load*" propagation="SUPPORTS" />
            <tx:method name="search*" propagation="SUPPORTS" />
        </tx:attributes>
    </tx:advice>

 

然后用Junit测试的时候总是报No Session found for current thread,这个错误,百度半天,各种解决方案丫滴都不管用,几近崩溃啊。。但是本着IT男的越挫越勇的精神,小弟还是坚持到最后,终于搞明白了。。。功夫不负有心人那。。。

如下是网上的一些解决方案:(what fucking。。。。根本不管用啊。。。)如下借鉴(http://hi.baidu.com/kaiby/item/adb9d49bae3bdbbdcc80e57d)3qqqq..

 

<prop key="hibernate.current_session_context_class">thread</prop>

 

 

<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>


这两个配置在最新的spring3和hibernate中4中都是多余的,

 

那是因为在spring事务管理中,current Session是绑定到 SpringSessionContext中的,而不是ThreadLocalSessionContext中的

而我的事务特性也是在spring配置了的,hibernate也交由了spring管理。spring真是个大管家啊,

参见这篇文章(很详细): http://blog.csdn.net/irelandken/article/details/7193123

 

所以以上的方法,在spring3+hibernate4中完全无效,请不要放弃治疗,修改一下配置的事务即可解决该问题。。。

<!-- 拦截器方式配置事物 -->
    <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="upd*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="modify*" propagation="REQUIRED" />
            <tx:method name="edit*" propagation="REQUIRED" />
            <tx:method name="del*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="remove*" propagation="REQUIRED" />
            <tx:method name="repair" propagation="REQUIRED" />
            <tx:method name="persistence*" propagation="REQUIRED" />

            <!-- hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到    -->
            <tx:method name="get*" propagation="REQUIRED" />
            <tx:method name="count*" propagation="REQUIRED" read-only="true" />
            <tx:method name="find*" propagation="REQUIRED" read-only="true" />
            <tx:method name="list*" propagation="REQUIRED" read-only="true" />
            <tx:method name="*" propagation="REQUIRED" />

        </tx:attributes>
    </tx:advice>

在以上的很多实验中出现了怪多的异常:

org.hibernate.HibernateException: No Session found for current thread

因为我在项目中一直都是作用的getCurrentSession,也没有在业务方法中进行事务管理,出现上面这个错误的原因就是在使用 getCurrentSession 的时候找不到相应的事务,所以No session就出来了,记住,并不是因为

<prop key="hibernate.current_session_context_class">thread</prop>

这个配置,所以一定要配置好事务管理。

在后面还出现了类似的:

org.hibernate.HibernateException: save is not valid without active transacti

只要把上面的问题解决了这个问题也解决了,

总结:使用最新的spring和hibernate记住干掉上面那两个配置和配置正确相应的Transaction。

 

 

 

还有一种解决方案就是:通过注解方式实现事务,也可以解决此问题,但是本人看过好多项目,这样做的比较少,暂时我也不太清楚,所以还得在学习下。。

<!-- 注解方式配置事物    -->
    <tx:annotation-driven transaction-manager="transactionManager" /> 

然后再Service中添加@Transactional注解即可。。。。

posted @ 2017-05-09 17:26  mapecun  阅读(238)  评论(0编辑  收藏  举报