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

對於提示*** 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都屬於這類。
 
下面是參考文章:

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對象,問題解決!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章