spring,hiberante之*** is not valid without active transaction

来源:互联网 发布:c语言0xff是什么意思 编辑:程序博客网 时间:2024/05/17 19:57
对于提示*** is not valid without active transaction 的错误
可以在Hibernate的配置文件中做如下修改

<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>(Hibernate4)
对于Hibernate3.x,可以直接把上述设置删除,就不会报错了,具体原因尚不清楚。
须知:几乎所有正常的操作都必须在transcation.isActive()条件下才能执行。get,load,save, saveOrUpdate,list都属于这类。
参考文章:http://www.cnblogs.com/williamsding/archive/2012/08/15/2639386.html
 
来自:http://blog.sina.com.cn/s/blog_6ba6492f0101d51t.html
下面是参考文章:
Hibernate Transaction 与 getCurrentSession() 关系

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对象,问题解决!

0 0
原创粉丝点击