org.springframework.transaction.TransactionSystemException: Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction not successfully started
最近在寫操作CLOB的過程中總是報(bào)這個異常,數(shù)據(jù)去插入進(jìn)去了.
先看看SessionFactory.getCurrentSession與openSession的區(qū)別
1. 如果使用的是getCurrentSession來創(chuàng)建session的話,在commit后,session就自動被關(guān)閉了,
也就是不用再session.close()了。但是如果使用的是openSession方法創(chuàng)建的session的話,
那么必須顯示的關(guān)閉session,也就是調(diào)用session.close()方法。這樣commit后,session并沒有關(guān)閉
/*2. getCurrentSession的使用可以參見hibernate\hibernate-3.2\doc\tutorial\src項(xiàng)目
3. 使用SessionFactory.getCurrentSession()需要在hibernate.cfg.xml中如下配置:
* 如果采用jdbc獨(dú)立引用程序配置如下:
<property name="hibernate.current_session_context_class">thread</property>
* 如果采用了JTA事務(wù)配置如下
<property name="hibernate.current_session_context_class">jta</property>*/
開始用的:
Session session = this.getHibernateTemplate().getSessionFactory().getCurrentSession();
Transaction tran=session.beginTransaction();
好多文章里都說只需在Spring配置<bean id="sessionFactory"...---><property name="hibernateProperties"> 中加入 <prop key="hibernate.current_session_context_class">thread</prop>就可以解決,試了沒用.
修改后的:
Session session = this.getHibernateTemplate().getSessionFactory().openSession();
Transaction tran=session.beginTransaction();
......
finally
{
session.close();
}
問題解決
posted on 2009-09-11 17:06
super_nini 閱讀(9743)
評論(6) 編輯 收藏