关于spring3中No Session found for current thread!and Transaction的配置和管理(转)
今天我是特别的郁闷,本来项目做到一半,以前都好好的,结果下午就出现问题,苦逼的到现在才解决。它出现问题的时候都一声不坑, 
,(天啦,现在才发现CSDN啥时候把QQ表情给整过来了)就在注册用户的时候,咦,后台发现咋SQL语句特么的不对劲,仔细一看数据根本就没有送到数据库去,只是简单的执行了一下查询操作,当时我就震惊了。首先就去看了Action是否没有写save方法,结果是没有任何错误。后来我再去再其他业务层是否也出现了问题,结果都能正常操作
,让我哭笑不得。后来只能查配置。当时我就看到这两个配置是在项目中同时出现的,天啦,我在惊叹,项目以前是怎么运行起来的。
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
这两个配置在最新的spring3.1.1和hibernate中4.1.3中都是多余的,
那是因为在Spring事务管理中,current Session是绑定到SpringSessionContext中的,而不是ThreadLocalSessionContext中的
而我的事务特性也是在spring配置了的,hibernate也交由了spring管理。spring真是个大管家啊,
参见这篇文章(很详细):http://blog.csdn.net/irelandken/article/details/7193123
<!--配置事务管理 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean> <!-- 配置事务特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="exists" read-only="true" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="merge*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="put*" propagation="REQUIRED" />
<tx:method name="use*" 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> <!-- 配置哪些类的方法进行事务管理 -->
<aop:config proxy-target-class="true">
<aop:pointcut id="bussinessService" expression="execution(* com.shop.service..impl.*.*(..))" />
<aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice" />
</aop:config>
<aop:config proxy-target-class="true">
<aop:pointcut id="dao" expression="execution(* com.shop.dao.*.*(..))" />
<aop:advisor pointcut-ref="dao" advice-ref="txAdvice" />
</aop:config>
在以上的很多实验中出现了怪多的异常:
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。
关于spring3中No Session found for current thread!and Transaction的配置和管理(转)的更多相关文章
- Hibernate4 No Session found for current thread原因
Hibernate4 与 spring3 集成之后, 如果在取得session 的地方使用了getCurrentSession, 可能会报一个错:“No Session found for curre ...
- SSH2 No Session found for current thread原因
Hibernate4 与 spring3 集成之后, 如果在取得session 的地方使用了getCurrentSession, 可能会报一个错:“No Session found for curre ...
- Hibernate4 No Session found for current thread
Hibernate4 与 spring3 集成之后, 如果在取得session 的地方使用了getCurrentSession, 可能会报一个错:“No Session found for curre ...
- hibernate在使用getCurrentSession时提示no session found for current thread
大致错误片段 org.hibernate.HibernateException: No Session found for current thread at org.springframework. ...
- Hibernate4集成spring4报错----No Session found for current thread
在编写一个Hibernate4集成spring4的小demo的时候出现了该错误: org.hibernate.HibernateException: No Session found for curr ...
- Spring+Hibernate4 Junit 报错No Session found for current thread
论坛上有另外一篇更全面的帖子,jinnianshilongnian写的:http://www.iteye.com/topic/1120924 本文的环境是: spring-framework-3.1 ...
- org.hibernate.HibernateException: No Session found for current thread
spring.springmvc和hibernate整合 在sessionFactory.getCurrentSession()时,出现以下异常 No Session found for curren ...
- spring+hibernate整合:报错org.hibernate.HibernateException: No Session found for current thread
spring+hibernate整合:报错信息如下 org.hibernate.HibernateException: No Session found for current thread at o ...
- SSH框架新线程下执行数据库持久化时 No Session found for current thread
架构:SSH框架 问题:多线程下的持久化操作 异常No Session found for current thread出现环境: SSH框架,采用声明式事务, 通过sessionFactory.ge ...
随机推荐
- 设计模式之Facade模式
Facade(外观)模式为子系统中的各类(或结构与方法)提供一个简明一致的界面,隐藏子系统的复杂性,使子系统更加容易使用.他是为子系统中的一组接口所提供的一个一致的界面. 在遇到以下情况使用Facad ...
- 重新温习,不但可以让自己对js更加的深刻认识,又能加深记忆,至此每天一次复习记录在此。
Undefined类型,它只有一个值就是它本身,打个比方你在声明变量的时候又未对变量赋值也就是相关初始化操作,那么此刻该变量的值便是Undefined. var a//声明一个变量. console. ...
- Hibernate占位符警告:use named parameters or JPA-style positional parameters instead.
Hibernate占位符警告:use named parameters or JPA-style positional parameters instead. >>>>> ...
- Gym 100187M-Heaviside Function
题意:给定函数: f(x) = θ(s1x - a1) + θ(s2x - a2) + ... + θ(snx - an), where si = ± 1. Calculate its values ...
- Ionic 弹出窗
Ionic弹窗服务允许程序创建.显示弹出窗口,需要用户继续响应. 弹窗系统支持更多灵活的构建alert(),prompt()和confirm()功能版本,以及用户习惯,除了允许查看完全自定义内容的的弹 ...
- 学习java随笔第十篇:java线程
线程生命周期 线程的生命周期:新建状态.准备状态.运行状态.等待/阻塞状态.死亡状态 示意图: 定义.创建及运行线程 线程: package threadrun; //定义一个实现Runnable接口 ...
- 简单登录案例(SharedPreferences存储账户信息)&联网请求图片并下载到SD卡(文件外部存储)
新人刚学习Android两周,写一个随笔算是对两周学习成果的巩固,不足之处欢迎各位建议和完善. 这次写的是一个简单登录案例,大概功能如下: 注册的账户信息用SharedPreferences存储: 登 ...
- T-SQL切割字符串方法小结 2
有表tb, 如下: id value ----------- ----------- 1 aa,bb 2 aaa,bbb,ccc 欲按id,分拆value列, 分拆后结果如下: id value -- ...
- git 常见问题收集(持续更新中)
1.问题:在配置完成github上的ssh后如何使用ssh? 答: 1)http方式 url =https://github.com/username/test_repo.git 2)ssh方式:把u ...
- CSS 导航栏
实例: 导航栏 Home News Articles Forum Contact About 导航栏 熟练使用导航栏,对于任何网站都非常重要. 使用CSS你可以转换成好看的导航栏而不是枯燥的HTML菜 ...