spring,hiberante之*** is not valid without active transaction
来源:互联网 发布:c语言0xff是什么意思 编辑:程序博客网 时间:2024/05/17 19:57
Spring 3.x 与Hibernate 4.x 整合遇到的问题,描述如下:
对数据库的 增加、删除、修改 操作需要Spring的事务支持,所以对service层的上述操作增加事务。applicationContext.xml配置如下:
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" read-only="false"/>
<tx:method name="save*" propagation="REQUIRED" read-only="false" />
<tx:method name="remove*" propagation="REQUIRED" read-only="false" />
<tx:method name="delete*" propagation="REQUIRED" read-only="false"/>
<tx:method name="modify*" propagation="REQUIRED" read-only="false"/>
<tx:method name="update*" propagation="REQUIRED" read-only="false" />
<tx:method name="*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
由于Spring 3.x 对 Hibernate 4.x 不提供 HibernateDaoSupport,所以在dao的实现层注入SessionFactory,从而通过
Session session = sessionFactory.getCurrentSession();
来获得当前的session。applicationContext.xm中 sessionFactory的HibernateProperties增加以下属性:
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
注意:和Spring2.x不同,不能为thread,否则报错:org.hibernate.HibernateException: save is not valid without active transaction
OK,以上配置在 增加、删除、修改 操作时,都能正确执行,事务也正常执行!
当执行 查询 操作时,不需要事务的支持,代码如下:
public List<Student> getStudentList(String str)
{
Session session = sessionFactory.getCurrentSession();
List<Student> list = null;
list = session.createQuery(str).list();
return list;
}
问题来了,报错:org.hibernate.HibernateException: No Session found for current thread
问题解决:几乎所有正常的操作都必须在transcation.isActive()条件下才能执行。get,load,save, saveOrUpdate,list都属于这类!
详情可以查看源码!
建议:当方法不需要事务支持的时候,使用 Session session = sessionFactory.openSession()来获得Session对象,问题解决!
- spring,hiberante之*** is not valid without active transaction
- spring,hiberante之*** is not valid without active transaction
- spring,hiberante之*** is not valid without active transaction
- spring,hiberante之*** is not valid without active transaction
- createQuery is not valid without active transaction
- getFlushMode is not valid without active transaction
- createQuery is not valid without active transaction
- createQuery is not valid without active transaction
- getHibernateFlushMode is not valid without active transaction;
- getFlushMode is not valid without active transaction
- spring 管理 hibernate 事务 load is not valid without active transaction
- spring声明式管理事务+ hibernate + save is not valid without active transaction
- hibernateTemplate不再出现:spring整合报错getFlushMode is not valid without active transaction
- spring+hibernate进行线程操作时异常:save is not valid without active transaction
- Spring 整合 hibernate 时,抛异常: createQuery is not valid without active transaction
- Spring管理Hibernate 报 createQuery is not valid without active transaction 错误
- spring和hibernate整合时异常(详解): getFlushMode is not valid without active transaction
- spring和hibernate整合时异常(详解): getFlushMode is not valid without active transaction
- 用Ant工具管理你的Web应用程序
- 内连接与外连接区别
- windows 7 64位 安装redis2.8.12 版本
- IOS开发工具——网络封包分析工具Charles
- 【数据结构】B树和B+树讲解
- spring,hiberante之*** is not valid without active transaction
- Go语言之环境搭建
- java线程同步volatile与synchronized
- UVA10878 - Decode the tape
- LightOJ1341 Aladdin and the Flying Carpet 约数的个数
- codeforces 710D Two Arithmetic Progressions
- NSRange、NSSize、NSPoint、NSRect
- c++文件io操作练习之写日志文件和读取参数配置文件
- 【3】windowNoTitle 和 android:windowNoTitle的区别